I have a model
class MyModel(ndb.Model):
foo = ndb.StringProperty()
class OtherModel(ndb.Model):
baz = ndb.StringProperty()
ref = ndb.KeyProperty(kind=MyModel)
I built a view where I can create and modify these model entities and I would like the ref
field of OtherModel
to be optional but when I try to submit and empty ref
field or remove the old value of it I got an error of the ndb.KeyProperty
's validation
My updating logic looks like this:
for property in editableProperties:
# Lets suppose this parses values from a form in a request to the needed type
new_value = deserialize(property._kind, self.request.get(property._name))
setattr(item, property._name, new_value)
Where I tried setting the new_value to None
and empty string. How can I make possible to do this?
If you don't have a Model
entity key to set the ref
property to just do not set it - i.e. do not include that property in the entity value that you're passing back to the datastore for writing. If you do, the property value you're passing will be subjected to sanity checks and they'll fail.
In other words: do not do at all
`setattr(item, property._name, new_value)`
for ref
when new_value
is not a key. Instead do something along these lines:
if hasattr(item, property._name):
delattr(item, property._name)