Let's say you define a Model with a ListProperty with a validator:

def ValidateTagNames(names):
 for name in names:
   if not IsValidTagName(name):
     raise db.BadValueError("Invalid tag name '%s'" % name)

class Item(db.Model):
 tags = db.StringListProperty(validator=ValidateTagNames)



Now, "validator" is only called from Property.validate, which is only
called by the descriptor method Property.__set__.  This means that if
you mutate a string list property's value with something like

 my_item.tags.append(new_tag)

the custom validator is *not* invoked.  Nor is it invoked at
Model.put() time.  It is, on the other hand, invoked when reading from
the datastore.  Thus, if you make the mistake of using a statement
like the one above, it will happily succeed, as will an ensuing
item.put() call... but any attempt to then read the saved object from
the datastore will raise an exception.

Admittedly, this is a bit of a "doctor, it hurts when I do that"
case... but it seems really error-prone to completely disallow
mutation of ListProperty values.  I've been working around it in my
app by overriding Item.put() with a method that does "self.tags =
self.tags", but this is an ugly hack (and also fails to work if you
use db.put).

One simple possibility would be to make the following change to
ListProperty.get_value_for_datastore:

--- a/__init__.py
+++ b/__init__.py
@@ -2295,8 +2295,11 @@ class ListProperty(Property):
    Returns:
      validated list appropriate to save in the datastore.
    """
-    return self.validate_list_contents(
+    value = self.validate_list_contents(
        super(ListProperty, self).get_value_for_datastore
(model_instance))
+    if self.validator:
+      self.validator(value)
+    return value


 class StringListProperty(ListProperty):


Does that seem reasonable?

--dave

--
glas...@davidglasser.net | langtonlabs.org | flickr.com/photos/glasser/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to