How to use __init__ with models?

42 views
Skip to first unread message

Santiago Basulto

unread,
Aug 24, 2011, 4:11:39 PM8/24/11
to google-a...@googlegroups.com
Hello,

i want to do some validation before my models start.

Something like

class NegativeDoorNumber(BaseException): pass

class Car(db.Model):
   doors = db.IntegerPropery(required=True,default=4)
   
   def __init__(self,doors=4,**kwds):
      if(doors<0):
         raise NegativeDoorNumber("Hey, doors can't be negative")
      db.Model.__init__(self,doors,kwds)

This, of course doesn't work. 

Can you help me?

Thanks!

Yanzheng Li

unread,
Aug 24, 2011, 4:17:58 PM8/24/11
to google-a...@googlegroups.com
Although this does not directly answer your question, it gives some good insight that may be benefitial to you.

Also, if you really want to have your custom constructor for validation, what you could do is to write a wrapper around your db.Model, and of course you can do whatever you want in your wrapper for the constructor.

Hope this helps.

Cheers.

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/6IlkEdipQtYJ.
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.



--
Yanzheng Li
Technical Solutions Engineer
Partner Solutions Organization
Google Inc.

Santiago Basulto

unread,
Aug 24, 2011, 4:21:04 PM8/24/11
to google-a...@googlegroups.com
Thank you for your answer Yanzheng.

Are you saying something like this?:

class CarFactory():
   def getNewCar(self,doors=4):
      if doors<0:
         raise ...
cf = CarFactory()
car = cf.getNewCar(doors = -1)

Yanzheng Li

unread,
Aug 24, 2011, 4:23:56 PM8/24/11
to google-a...@googlegroups.com
Hi:

Yes, something like this.

Give it a try first to see if it works well. I haven't actually done it myself so i cannot guarantee.

Cheers.

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/FaKiVwjyX6QJ.

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.

Santiago Basulto

unread,
Aug 24, 2011, 4:26:32 PM8/24/11
to google-a...@googlegroups.com
I was doing that. But it seems weird. I'd like to have everything in one piece. I'd like to make my models "intelligent pieces" not just "data holders". You know what i mean?

Yanzheng Li

unread,
Aug 24, 2011, 4:33:18 PM8/24/11
to google-a...@googlegroups.com
class CarWrapper(object):

    def __init__(self,doors=4,**kwds):
       if door < 0:
           throw Exception() # throw whatever exception you want here.
       else:
           self.car = Car(door) # instantiate your db.Model instance.

       # This is an example of your own getter to expose the properties of your underlying db.Model instance.
       def GetDoorNumber(self):
           if self.car:
               return self.car.doors

This may not be the best approach, but this is what I can think of for now. The drawback is that you need to add your own getters/setters for each property of your underlying db.Model instance that you want to expose, so it can get very cumbersome if you have a lot of properties/models. But if you just have a few it shouldn't be too bad to use.

Hope this helps.



On Wed, Aug 24, 2011 at 1:26 PM, Santiago Basulto <santiago...@gmail.com> wrote:
I was doing that. But it seems weird. I'd like to have everything in one piece. I'd like to make my models "intelligent pieces" not just "data holders". You know what i mean?

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/IFThVHNdAj0J.

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.
360.gif

Santiago Basulto

unread,
Aug 24, 2011, 4:35:38 PM8/24/11
to google-a...@googlegroups.com
Thank you very much brother!

Tim Hoffman

unread,
Aug 24, 2011, 7:01:47 PM8/24/11
to google-a...@googlegroups.com
HI

I REALLY wouldn't go down this path, unless you understand a great deal more about the inner workings of db.Model

__init__ gets called when objects are retrieved from the datastore as well.

ALso something to consider, if your business rules changes over time and you decide to change them
and existing entities don't comply with the rule, then you won't be able to retrieve them from the datastore.

I would instead consider using properties designed to enforce the business rules, 
or use factories to create the entities that check all the rules based on the data supplied before creating the entity

You might find it valuable to read some of Nick Johnsons blog posts on custom properties

Rgds

Tim

sebastián serrano

unread,
Aug 24, 2011, 7:29:00 PM8/24/11
to google-a...@googlegroups.com
Hi 
  I think this article from the app engine blog has a more clear description of custom properties for this usage:

Cheers, Sebastian

Santiago Basulto

unread,
Aug 24, 2011, 7:37:15 PM8/24/11
to google-a...@googlegroups.com
Yes, i think that's the best way to do it.

Thank you both.

El día 24 de agosto de 2011 20:29, sebastián serrano
<sebastia...@gmail.com> escribió:

> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/google-appengine/-/KqHkF0NXxhgJ.


> 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.
>

--
Santiago Basulto.-

Robert Kluin

unread,
Aug 25, 2011, 1:00:22 AM8/25/11
to google-a...@googlegroups.com
If you decide to go the factory route, you could always make the
factory a class method.
http://docs.python.org/library/functions.html#classmethod

class YourModel(db.model):
@classmethod
def create_new(cls, **kwargs):
# check args... raise exception / return None on error
return cls(**kwargs)


new_model = YourModel.create_new(this='that', some='thing')

Reply all
Reply to author
Forward
0 new messages