Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why required and default are mutally exclusive in ndb?

In old google appengine datastore API "required" and "default" could be used together for property definitions. Using ndb I get a

ValueError: repeated, required and default are mutally exclusive.

Sample code:

from google.appengine.ext import ndb
from google.appengine.ext import db

class NdbCounter(ndb.Model):
    # raises ValueError
    count = ndb.IntegerProperty(required=True, default=1)

class DbCounter(db.Model):
    # Doesn't raise ValueError
    count = db.IntegerProperty(required=True, default=1)

I want to instantiate a Counter without having to specify a value. I also want to avoid someone to override that value to None. The example above is constructed. I could probably live without a required attribute and instead add an increment() method. Still I don't see the reason why required and default are mutually exclusive.

Is it a bug or a feature?

like image 284
bastian Avatar asked Jan 09 '13 08:01

bastian


1 Answers

I think you are right. Perhaps I was confused when I write that part of the code. It makes sense that "required=True" means "do not allow writing the value None" so it should be possible to combine this with a default value. Please file a feature request in the NDB tracker: http://code.google.com/p/appengine-ndb-experiment/issues/list

Note that for repeated properties things are more complicated, to repeated will probably continue to be incompatible with either required or default, even if the above feature is implemented.

like image 165
Guido van Rossum Avatar answered Oct 15 '22 09:10

Guido van Rossum