db.model and python constructors

127 views
Skip to first unread message

Kelly A

unread,
Apr 10, 2010, 12:10:05 PM4/10/10
to Google App Engine
As I understand it you cannot use python constructors when creating an
object that inherits from db.model, so this would not work:

class myClass(db.model):
myProperty = db.StringProperty()
def __init__(self):
tempProperty = something complex.
myProperty = tempProperty


Is the best way to do processing on the creation of objects based on
db.model to create factories that generate the objects then?

david ignacio

unread,
Apr 10, 2010, 1:13:19 PM4/10/10
to google-a...@googlegroups.com
think about something like this:

class myClass(db.model):
myProperty = db.StringProperty()

def __init__(self, myProperty=None, **kwargs):
if myProperty is None:
myProperty = something cool
super(myClass, self).__init__(myProperty=myProperty, **kwargs)

since what you are doing is replacing the db.model constructor, not
actually calling it anywhere

> --
> 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-a...@googlegroups.com.
> To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
>
>

Nick Johnson (Google)

unread,
Apr 12, 2010, 5:53:15 AM4/12/10
to google-a...@googlegroups.com
Hi Kelly,

It is possible to provide a constructor, but not recommended. If you do, make sure to take arbitrary arguments (*args, **kwargs) and pass them to the parent constructor, and bear in mind that the constructor will be called internally when constructing instances restored from the datastore, as well as when you call it explicitly.

In general, though, factory methods are a safer option.

-Nick Johnson

--
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-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.




--
Nick Johnson, Developer Programs Engineer, App Engine Google Ireland Ltd. :: Registered in Dublin, Ireland, Registration Number: 368047
Google Ireland Ltd. :: Registered in Dublin, Ireland, Registration Number: 368047
Reply all
Reply to author
Forward
0 new messages