Setting up an Initialiser

7 views
Skip to first unread message

eli

unread,
Jan 16, 2009, 11:02:45 AM1/16/09
to Google App Engine
Hi,

I cant complete something seemingly very simple.
I simply want to create the following object (Auction)

auction =Auction(title, desc)


class Auction(db.Model):

title = db.StringProperty()
description = db.StringProperty()

def __init__(self, title, desc):
self.title = title
self.desc = desc

This code is trimmed down from the full version, could you tell me if
im doing anything fundamentally wrong with the __init__ function.

Thank you for your time,

Elliott


App Engine reports:
AttributeError: 'Auction' object has no attribute '_entity'

Qian Qiao

unread,
Jan 16, 2009, 12:25:01 PM1/16/09
to google-a...@googlegroups.com
On Sat, Jan 17, 2009 at 00:02, eli <elliott....@gmail.com> wrote:
>
> Hi,
>
> I cant complete something seemingly very simple.
> I simply want to create the following object (Auction)
>
> auction =Auction(title, desc)
>
>
> class Auction(db.Model):
>
> title = db.StringProperty()
> description = db.StringProperty()
>
> def __init__(self, title, desc):
> self.title = title
> self.desc = desc

You don't generally need to touch the __init__ function of your
models, You use them like this:

class Auction(db.Model):
title = db.StringProperty()
description = db.StringProperty()

Then when you need your aution model, you just do:

auction = Auction(title="Title", description="description")

HTH.

-- Joe

djidjadji

unread,
Jan 16, 2009, 12:27:22 PM1/16/09
to google-a...@googlegroups.com
If you look at the example at

http://code.google.com/appengine/docs/python/gettingstarted/usingdatastore.html

you see that you don't write an __init__() method if you subclass from
db.Model or db.Expando.
db.Model.__init__() will take care of assigning the correct attributes.

Elliott Rosenthal

unread,
Jan 16, 2009, 12:49:28 PM1/16/09
to google-a...@googlegroups.com
What about if I wish to initialise the variables inside the class with
a complex calculation?
The default parameter wont suffice, is there any other way?

2009/1/16 djidjadji <djid...@gmail.com>:

Qian Qiao

unread,
Jan 16, 2009, 1:00:08 PM1/16/09
to google-a...@googlegroups.com
On Sat, Jan 17, 2009 at 01:49, Elliott Rosenthal
<elliott....@gmail.com> wrote:
>
> What about if I wish to initialise the variables inside the class with
> a complex calculation?
> The default parameter wont suffice, is there any other way?

Define a factory method that builds the model?

-- Joe

Elliott Rosenthal

unread,
Jan 16, 2009, 1:02:25 PM1/16/09
to google-a...@googlegroups.com
Thank you for the help so far, it really has been useful.
If there any way you could give me a quick snippet of code to show me
how to build a factory method?


2009/1/16 Qian Qiao <qian...@gmail.com>:

Qian Qiao

unread,
Jan 16, 2009, 1:11:18 PM1/16/09
to google-a...@googlegroups.com
On Sat, Jan 17, 2009 at 02:02, Elliott Rosenthal
<elliott....@gmail.com> wrote:
>
> Thank you for the help so far, it really has been useful.
> If there any way you could give me a quick snippet of code to show me
> how to build a factory method?

Let's say we have a model that models a circle, and you were to
calculate the area of the circle in your __init__ method, we can use
the following as an alternative:

class Circle(db.Model):
centre = db.GeoPtProperty()
radius = db.IntegerProperty()
area = db.FloatProperty()

@staticmethod
def build_circle(centre, radius):
area = math.pi * radius * radius # example of the complex calculation
return Circle(centre = centre, radius = radius, area = area)

So when you need a circle somewhere in you code, instead of creating
it like circle = Circle(centre, radius), you do it as: circle =
Circle.build_circle(centre, radius)

HTH

-- Joe

Elliott Rosenthal

unread,
Jan 16, 2009, 1:15:15 PM1/16/09
to google-a...@googlegroups.com
Yes of course!
:) Thank you for the help.

Saved me a lot of sweat.

2009/1/16 Qian Qiao <qian...@gmail.com>:

Andy Freeman

unread,
Jan 16, 2009, 1:36:05 PM1/16/09
to Google App Engine
> class Auction(db.Model):
>
> title = db.StringProperty()
> description = db.StringProperty()
>
> def __init__(self, title, desc):
> self.title = title
> self.desc = desc
>
> This code is trimmed down from the full version, could you tell me if
> im doing anything fundamentally wrong with the __init__ function.

Yes, you are doing something fundamentally wrong. If you define an
__init__ function, db.Model's __init__ won't be called unless you call
it and things won't work if db.Model's __init__ isn't called.

Instead, you want something like

def __init__(self, *args, **kwds):
{your code}
super(Auction, self).__init__(*args, **kwds)
{more of your code}

For example, {your code} could include "kwds.setdefault("title",
"default title")" (Yes, this is the dumb/wrong way to do default
values.)

If you have named arguments, make sure that you pass the right
information to the super's __init__.

Note also that your __init__ will be called when the runtime
constructs an Auction, as it does for query and get results.

FWIW, it's likely that you shouldn't be writing your own __init__
function.
Reply all
Reply to author
Forward
0 new messages