开发者

Required custom Property

I create a custom db.Property subclass:

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)

Now I also create a db.Model subclass:

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

So I test them out:

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

I should expect an error like:

BadValueError: Property foo is required

How do I make sure that my custom Property subclass, when it is required, raises a BadValueError? Am I missing something in my PropertyFoo definition?


I believe you need to call the validate method of the super class:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜