views:

24

answers:

1

Hi there,

I need to model the following situation and I can't seem to find a consistent example on how to do it "correctly" for the google app engine.

Suppose I've got a simple situation like the following:

Company 1 -----> M Store

A company has one to many stores. Each store has an address made up of a address line 1, city, state, country, postcode etc.

Ok. Lets say we need to create say an "Audit". An Audit is for a company and can be across one to many stares.

So something like:

Audit 1 ------> 1 Company
      1 ------> M Store

Now we need to query all of the "audits" based on the Store "addresses" in order to send the "Auditors" to the right locations.

There seem to be numerous articles like this one:

http://code.google.com/appengine/articles/modeling.html

Which give examples of creating a "ContactCompany" model class. However they also say that you should use this kind of relationship only when you "really need to" and with "care" for performance.

I've also read - frequently - that you should denormalize as much as possible thereby moving all of the "query-able" data into the Audit class.

So what would you suggest as the best way to solve this?

I've seen that there is an Expando class but I'm not sure if that is the "best" option for this.

Any help or thoughts on this would be totally appreciated.

Thanks in advance, Matt

A: 
class Company(db.Model):
   name = db.StringProperty()

class address(db.Model):
   address = db.PostalAddressProperty()

class store(db.Model):
   company = db.ReferenceProperty(Company)
   address = db.ReferenceProperty(Address)

now you can query for all company store by,

company_oject.address_set.all().filter().fetch()

unless it is MNC with thousands of store, this query should work fine in most cases.

http://code.google.com/appengine/docs/python/datastore/entitiesandmodels.html#References

helps further.

iamgopal
THanks for your answer.
Sway