建模实体关系:一对多

时间:2014-07-01 22:03:20

标签: google-app-engine google-cloud-datastore

我正在跟踪google文档https://developers.google.com/appengine/articles/modeling

One to Many的{​​{1}}关系
Modeling Entity Relationships

如何查询class Contact(db.Model): # Basic info. name = db.StringProperty() birth_day = db.DateProperty() # Address info. address = db.PostalAddressProperty() # The original phone_number property has been replaced by # an implicitly created property called 'phone_numbers'. # Company info. company_title = db.StringProperty() company_name = db.StringProperty() company_description = db.StringProperty() company_address = db.PostalAddressProperty() class PhoneNumber(db.Model): contact = db.ReferenceProperty(Contact, collection_name='phone_numbers') phone_type = db.StringProperty( choices=('home', 'work', 'fax', 'mobile', 'other')) number = db.PhoneNumberProperty() scott = Contact(name='Scott') scott.put() PhoneNumber(contact=scott, phone_type='home', number='(650) 555 - 2200').put() PhoneNumber(contact=scott, phone_type='mobile', number='(650) 555 - 2201').put() 以获取号码(650) 555 - 2201的所有者?

1 个答案:

答案 0 :(得分:0)

您可以查询(650)555 - 2201以获取数字scott的所有权,如下所示:

phone = PhoneNumber.all()。filter(' number =','(650)555 - 2201')

print'%s' %(phone.contact.name)