1

我创建了一个自定义db.Property子类:

class PropertyFoo(db.StringProperty):
  def validate(self, value):
    # Yes this is a pointless validator, but please read on
    return value

  def get_value_for_datastore(self, model_instance):
    return super(PropertyFoo, self).get_value_for_datastore(model_instance)

  def make_value_from_datastore(self, value):
    return super(PropertyFoo, self).make_value_from_datastore(value)

现在我还创建了一个db.Model子类:

class ModelFoo(db.Model):
  foo = PropertyFoo(required=True)

所以我测试了它们:

>>> m = ModelFoo()
# No errors :(
>>> m.foo
# No errors still
>>> print m.foo
'None' # <-- Why???

我应该期待一个错误,如:

BadValueError: Property foo is required

我如何确保我的自定义Property子类,当它是时required,会引发一个BadValueError?我在我的PropertyFoo定义中遗漏了什么吗?

4

1 回答 1

3

我相信你需要调用validate超类的方法:

class PropertyFoo(db.StringProperty):
  def validate(self, value):
    super(PropertyFoo, self).validate(value) #Base implementation checks
    #Do your other check here raising BadValueError
    return value
于 2011-08-04T10:53:00.460 回答