define inheritance relationship between kinds in GAE

60 views
Skip to first unread message

Bruce Aloe

unread,
Jun 14, 2011, 3:29:39 AM6/14/11
to Google App Engine
Hello,

I want to define two kinds (tables): Person (PersonId, age, name) and
Teacher (PersonId, age, name, teaching experience) in my GAE datastore
and Person is the superclass of Teacher. I want to define inheritance
relationship between Person and Teacher, so Teacher kind inherites
PersonId, age, name columns (attributes) from Person kind.

Is there any way to define it in python environment from GAE?

Thanks!

Bruce

Ikai Lan (Google)

unread,
Jun 14, 2011, 3:44:43 AM6/14/11
to Google App Engine
Better implementation: define a Person model and a fields Text type where you store additional data as a JSON object. Store a "type" attribute. Then wrap the Person class with Teacher for all types that have the "Teacher" attribute. 

Ikai Lan 
Developer Programs Engineer, Google App Engine



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


Ross M Karchner

unread,
Jun 14, 2011, 7:10:10 AM6/14/11
to google-a...@googlegroups.com
Ikai, isn't this about what Polymodel does?
--
Ross M Karchner


Michael

unread,
Jun 14, 2011, 8:55:23 AM6/14/11
to google-a...@googlegroups.com
Hi Bruce,

I believe what you are trying to achieve is best accomplished using the db.ReferenceProperty(Person).

class Person(db.Model):
  name= db.StringProperty()
  addressStreet= db.StringProperty()
  email = db.StringProperty()

class Teacher(db.Model):
  employeeNumber = db.StringProperty()
  person = db.ReferenceProperty(Person)

Then the reference would be to teacher.person.name

Michael

Owen

unread,
Jun 15, 2011, 2:42:56 AM6/15/11
to Google App Engine
Ikai

Could you expand on this example a bit, or provide a link that
discusses it? I'm very interested in best practices for datastore
structure. I've read a number of comments that indicate that
ReferenceProperty implementations can be datastore intensive and that
it's better to find creative ways to denormalize data. This sounds
like one of them, but I'm having trouble visualizing what you describe
here.

Thanks,
Owen

Ikai Lan (Google)

unread,
Jun 15, 2011, 9:05:22 AM6/15/11
to Google App Engine
Maybe. I don't use PolyModel. Mostly because I think people go a little too overboard with OO.

Ikai Lan 
Developer Programs Engineer, Google App Engine


Ikai Lan (Google)

unread,
Jun 15, 2011, 9:06:41 AM6/15/11
to Google App Engine
The most brief explanation I can offer is this: think of the datastore as a giant key-value store. Design your persistence to this model as much as possible such that you can do as much using get by key. Minimize the total phases of RPCs - remember that you can batch get keys. Use queries sparingly and only when there's no way to do what you want to do using keys.

Ikai Lan 
Developer Programs Engineer, Google App Engine


Ikai Lan (Google)

unread,
Jun 15, 2011, 9:06:58 AM6/15/11
to Google App Engine
Oh ... and use entity groups.

Ikai Lan 
Developer Programs Engineer, Google App Engine


Joops

unread,
Jun 15, 2011, 12:17:45 PM6/15/11
to Google App Engine
Hello,

Sorry, wouldn't normally ask something that I am sure I could google
for, but as it is relevant to the discussion I thought I would pop it
here.

Ikai, you mentioned using a JSON Object to store rich information. I
can understand the why, I just wandered about the how.

Would the answer in this post be a "best practice" (use the Django
json lib)
http://stackoverflow.com/questions/1171584/how-can-i-parse-json-in-google-app-engine


Thanks very much

J

On Jun 15, 2:06 pm, "Ikai Lan (Google)" <ika...@google.com> wrote:
> Oh ... and use entity groups.
>
> Ikai Lan
> Developer Programs Engineer, Google App Engine
> Blog:http://googleappengine.blogspot.com
> Twitter:http://twitter.com/app_engine
> Reddit:http://www.reddit.com/r/appengine
>
> On Wed, Jun 15, 2011 at 9:06 PM, Ikai Lan (Google) <ika...@google.com>wrote:
>
>
>
> > The most brief explanation I can offer is this: think of the datastore as a
> > giant key-value store. Design your persistence to this model as much as
> > possible such that you can do as much using get by key. Minimize the total
> > phases of RPCs - remember that you can batch get keys. Use queries sparingly
> > and only when there's no way to do what you want to do using keys.
>
> > Ikai Lan
> > Developer Programs Engineer, Google App Engine
> > Blog:http://googleappengine.blogspot.com
> > Twitter:http://twitter.com/app_engine
> > Reddit:http://www.reddit.com/r/appengine
>
> >>http://groups.google.com/group/google-appengine?hl=en.- Hide quoted text -
>
> - Show quoted text -

Ernesto Oltra

unread,
Jun 16, 2011, 1:02:42 AM6/16/11
to google-a...@googlegroups.com
Simply you have to put this on your model:

class MyModel(db.Model):
  ...
  mydata = JSONProperty()
  ...

And write mydata as you want, for example:

mymodel.mydata = {
  'hello': 'world',
  'bye': 'world',
}

The same for reading:

mymodel.mydata['hello'] # == 'world'
mymodel.mydata['bye'] # == 'world'


This is the code for JSONProperty:

from google.appengine.ext import db
import simplejson

class JSONProperty(db.TextProperty):
  def validate(self, value):
    return value

  def get_value_for_datastore(self, model_instance):
    result = super(JSONProperty, self).get_value_for_datastore(model_instance)
    result = simplejson.dumps(result)
    return db.Text(result)

  def make_value_from_datastore(self, value):
    try:
      value = simplejson.loads(str(value))
    except:
      pass

    return super(JSONProperty, self).make_value_from_datastore(value)

Owen

unread,
Jun 16, 2011, 3:55:34 AM6/16/11
to Google App Engine
All,

Thanks for posting the information and examples. This is very helpful!

Owen
Reply all
Reply to author
Forward
0 new messages