python - Google App Engine 中模型的默认值

标签 python google-app-engine model default-value

是否可以为模型设置默认值?例如,考虑 Appengine 文档中的这个模型

from google.appengine.ext import db

class Pet(db.Model):
    name = db.StringProperty(required=True)
    type = db.StringProperty(required=True, choices=set(["cat", "dog", "bird"]))
    birthdate = db.DateProperty()
    weight_in_pounds = db.IntegerProperty()
    spayed_or_neutered = db.BooleanProperty()
    owner = db.UserProperty(required=True)

我想将name 的默认值设置为“Unnamed Pet”,因此如果用户不提供,则采用默认值。这可能吗?

PS:我希望这在模型类 Pet 本身中完成

最佳答案

使用默认属性,例如

class Pet(db.Model):
    name = db.StringProperty(required=True, default="(unnamed)") 

关于python - Google App Engine 中模型的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8551602/

相关文章:

python - 为什么多个多处理 imap 会阻塞?

python - (Python) shebang 太长

java - Google App Engine/del 网址错误

google-app-engine - 在 Google App Engine 上备份数据的最佳方式是什么?

android - 在模型对象中引用 Strings.XML 以支持本地化

asp.net - TryUpdateModelAsync() 无法传入对象参数进行更新

asp.net-mvc - 表达式树可能不包含动态操作 - mvc

python - 应用程序尚未准备好,升级到 Django 1.9,然后升级到 2.0

python - 当我使用 pandas statsmodels 时,出现断言错误

python - 谷歌应用引擎 NDB : How to store document structure?