pombreda / appengine-ndb-experiment

Automatically exported from code.google.com/p/appengine-ndb-experiment
Other
0 stars 0 forks source link

why not allow callable default values? #198

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Currently property's defaults have to be values.

Why not allow the same as in django: if you set the default to a callable and 
don't pass a value for that property to init, the callable is called to init 
the property.
https://docs.djangoproject.com/en/dev/ref/models/fields/#default

Original issue reported on code.google.com by joernhees2 on 9 Aug 2012 at 5:29

GoogleCodeExporter commented 9 years ago
Yes, why not. :-)

TBH I find Django's tendency to have "optionally a callable" everywhere in its 
API a liability. Do you have a use case where it really is a big deal to 
manually set a computed initial value? If so, I'd like to know more about it.

Original comment by guido@google.com on 13 Aug 2012 at 2:01

GoogleCodeExporter commented 9 years ago
In my current project I wanted to assign a randomly initialized float (0<= f <= 
1) for a "random article" functionality. I had the feeling it was better not to 
bother the dev using that model with a comment on how to initialize that random 
field and why.
Other examples: default Usernames, random CSRF tokens, API Keys...

I have the feeling if not overused it really can make the code less verbose in 
many cases.

Original comment by joernhees2 on 15 Aug 2012 at 9:19

GoogleCodeExporter commented 9 years ago
It almost makes me thing we need a post-create hook that is called when a new 
entity is created by the app calling the constructor or by get_or_insert(), but 
not when it is read from the datastore.

Original comment by guido@google.com on 15 Aug 2012 at 9:32