开发者

GAE modeling relationship options

开发者 https://www.devze.com 2022-12-26 02:43 出处:网络
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

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 th开发者_如何学编程e "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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消