ã¨ã³ãã£ãã£ã®é¢é£ãã¢ããªã³ã°ãã
http://code.google.com/appengine/articles/modeling.html
ã¯ããã«
ãããã«Getting Started Guideã¯åç´ãªAppEngineã¢ãã«ã®ããããã£ãåããã®ã«å¿ è¦ãªãã¨ãæãã¦ããããã©ããããã¼ã¿ã¹ãã¢ã«ç¾å®ä¸çã®ä½ãã表ç¾ã§ããããã«ãªãããã¨è¨ããªãããã ããã足ããªããã¦ã§ãã¢ããªã±ã¼ã·ã§ã³éçºã®åå¿è ã§ãããã¨ãSQLãã¼ã¿ãã¼ã¹ã«é¦´æãã§ãããã¨ããã®è¨äºã¯AppEngineã®ãã¼ã¿è¡¨ç¾ã®æ¬¡ã®é åã¸é²ã¿ããå ¨ã¦ã®äººã®ããã«æ¸ããã¦ããã
ã¨ã³ãã£ãã£ã«é¢é£ãå¿ è¦ãªã®ã¯ãªãã
ã¦ã¼ã¶ã¼ãé£çµ¡å ãä¿åã§ããã¢ãã¬ã¹å¸³ãæã¤ããã£ãããã¦æ°ããã¦ã§ãã¢ããªã±ã¼ã·ã§ã³ãæ§ç¯ä¸ã ã¨èããããã¦ã¼ã¶ãä¿åããé£çµ¡å ã®ããã«ãç¸æã®ååãèªçæ¥(絶対å¿ãã¡ããããªã)ãä½æãé»è©±çªå·ãä¼ç¤¾ãåå¾ãããã
ã¦ã¼ã¶ãä½æã追å ãããã¨æãã¨ããã©ã¼ã ã«æ å ±ãå ¥åãã次ã®ãããªæãã®ã¢ãã«ã«ãã®æ å ±ãä¿åããã:
class Contact(db.Model): # Basic info. name = db.StringProperty() birth_day = db.DateProperty() # Address info. address = db.PostalAddressProperty() # Phone info. phone_number = db.PhoneNumberProperty() # Company info. company_title = db.StringProperty() company_name = db.StringProperty() company_description = db.StringProperty() company_address = db.PostalAddressProperty()
ä¸åºæ¥ã ãã¦ã¼ã¶ã¯ããã«ãã®ã¢ãã¬ã¹ããã¯ã使ãå§ãããã¼ã¿ã¹ãã¢ã¯ããã«åã¾ãå§ããã ãããããã¦ãã®æ°ã¢ããªã±ã¼ã·ã§ã³ããããã¤ãã¦ãã°ããããã¨ãã¦ã¼ã¶ãé»è©±çªå·ãã²ã¨ã¤ããæã¦ãªããã¨ãä¸æºã«æã£ã¦ããã¨ç¥ããã¨ã«ãªãããã人ã®å®¶ã®çªå·ã ããããªãä¼ç¤¾ã®çªå·ãè¨é²ãããã¨ãã¯ã©ãããã°ãããã ï¼åé¡ãªãããã¨æããåã¯ãã¼ã¿æ§é ã«ä¼ç¤¾ã®é»è©±çªå·ãä»ãå ãããã¤ã¾ã次ã®ããã«ã :
# Phone info.
phone_number = db.PhoneNumberProperty()
work_phone_number = db.PhoneNumberProperty()
ãã©ã¼ã ã«æ°ãããã£ã¼ã«ããä»ãå ããã¨åã¯ä»äºã«æ»ããã¢ããªã±ã¼ã·ã§ã³ãåã³ãããã¤ããã¨ããã«ããããã®ä¸æºãèããã¨ã«ãªãã ãããæ°ããé»è©±çªå·ãã£ã¼ã«ããå¢ãããã¨ãç¥ãã¨ãããã«ã»ãã®ãã£ã¼ã«ããå¸æãå§ãããFAXçªå·ãã»ããã¨ãã人ãããã°ãæºå¸¯çªå·ãã»ããã¨ãã人ããããæºå¸¯ãè¤æ°æã£ã¦ã人ã ã£ã¦ããã ãã(ãã¾ã©ãã®åä¾ãã¡ã¯ãã³ã大å¿ãã )ï¼FAXãæºå¸¯ç¨ã®ãã£ã¼ã«ããä¸ã¤äºã¤è¿½å ãããã¨ãã§ãããã ãã©æºå¸¯ã3ã¤æã£ã¦ããã©ãããï¼10åã ã¨ï¼èª°ããåãèãããã¨ããªããããªé»è©±ãçºæããã¨ï¼
ãã®ã¨ããã¢ãã«ã«ã¯é¢é£ãå¿ è¦ã«ãªãã
ä¸å¯¾å¤
é£çµ¡å ããããã«ã¤ãã¦å¥½ããªã ãããããã®é»è©±çªå·ãè¨å®ã§ããããã«ããããã®çãã¯ããã ããã®ããã«ã¯é»è©±çªå·ããèªèº«ãããããã¯ã©ã¹ã¨é£çµ¡å ä¸ã¤ã«è¤æ°ã®é»è©±çªå·ãé¢é£ä»ããæ段ãå¿ è¦ã«ãªããReferencePropertyã使ãã°ä¸å¯¾å¤é¢é£ã®ã¢ãã«åã¯ç°¡åã ãæ°ããã¯ã©ã¹ã¯ããã¨ãã°ãããªãã ãã:
class Contact(db.Model): # Basic info. name = db.StringProperty() birth_day = db.DateProperty() # Address info. address = db.PostalAddressProperty() # å ã®phone_numberããããã£ã¯ãæé»çã«ä½æããã # '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()
ãã®æ¹æ³ã®ãã¼ã«ãªãã®ã¯contactããããã£ã ãReferencePropertyã¨ãã¦å®ç¾©ãããã¨ã§ãContactåã®å¤ã ãã代å
¥ã§ããããããã£ãå®ç¾©ã§ãããåç
§åã®ããããã£ãå®ç¾©ããã¨åç
§ãããã¯ã©ã¹ã«æé»çãªã³ã¬ã¯ã·ã§ã³ããããã£ãä½æããããããã©ã«ãã§ã¯ãã®ã³ã¬ã¯ã·ã§ã³ã¯
é£çµ¡å ã¨ãé»è©±çªå·ãä¸ã¤é¢é£ä»ããã®ã¯ç°¡åã ã"Scott"ã¨ããé£çµ¡å ã家ã¨æºå¸¯ã®äºã¤ã®é»è©±çªå·ãæã¤ã¨ãããããã®é£çµ¡å ã¯æ¬¡ã®ããã«ä½æã§ãã:
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()
ReferencePropertyãContactã«ç¹æ®ãªããããã£ã追å ãã¦ããã¦ããã®ã§ããã人ç©ãä¸ããããã¨ãã«é¢ä¿ãããã¹ã¦ã®é»è©±çªå·ã¯ç°¡åã«æã«å ¥ãããã人ç©ã®ãã¹ã¦ã®çªå·ã表示ããããã°ã次ã®ããã«ããã°ããã
print 'Content-Type: text/html' print for phone in scott.phone_numbers: print '%s: %s' % (phone.phone_type, phone.number)
ãã®çµæã¯æ¬¡ã®ããã«ãªã:
home: (650) 555 - 2200 mobile: (650) 555 - 2201
注: ããã©ã«ãã§ã¯ãã®ç¨®ã®é¢é£ã§ã¯é åºã¯ä¿æãããªãã®ã§ãåºåã®é åºã¯ç°ãªããããããªã
phone_numbersä»®æ³å±æ§ã¯Queryã®ã¤ã³ã¹ã¿ã³ã¹ãªã®ã§ãContactã«é¢ããã³ã¬ã¯ã·ã§ã³ãããã«çµãè¾¼ãã ãã½ã¼ããããã§ããããã¨ãã°ã家ã®çªå·ã ããã»ãããã°æ¬¡ã®ããã«ãã:
scott.phone_numbers.filter('phone_type =', 'home')
Scottãé»è©±ããªãããã¨ãã¯ãåã«ã¬ã³ã¼ããåé¤ããã°ãããPhoneNumberã¤ã³ã¹ã¿ã³ã¹ãåé¤ããã ãã§ããã¯ã¨ãªã«ã¯ç¾ããªããªãã
jack.phone_numbers.filter('phone_type =', 'home').get().delete()
å¤å¯¾å¤
ã¦ã¼ã¶ãé£çµ¡å ãã°ã«ã¼ãã«ã¾ã¨ãã¦ç®¡çã§ããããã«ãããã¨ãããã°ã«ã¼ãã¨ãã¦ã¯ãå人ããååãã家æããªã©ãããã ãããã¦ã¼ã¶ã¯ãããã®ã°ã«ã¼ããã¾ã¨ãã¦ä½ãã®å¦çããã¨ãã°å人ãã¹ã¦ã«ããã«ã½ã³ã®æå¾ ç¶ãéã£ããã¨ãããã§ãããã¾ãã¯ç°¡åã«æ¬¡ã®ãããªGroupã¢ãã«ãå®ç¾©ããã:
class Group(db.Model): name = db.StringProperty() description = db.TextProperty()
Contactã«ã¯æ°ããgroupã¨ããååã®ReferencePropertyã追å ãããã¨ãã§ããããããããã¨é£çµ¡å ã¯ãã£ãä¸ã¤ã®ã°ã«ã¼ãã«ããæå±ã§ããªããªãããã¨ãã°ååã§ããå人ã§ãããããªäººãããã ãããå¤å¯¾å¤é¢é£ã表ç¾ããæ¹æ³ãå¿ è¦ã ã
ãã¼ã®ãªã¹ã
ã¨ã¦ãç°¡åãªããæ¹ã¯ãé¢é£ã®ä¸æ¹ããã¼ã®ãªã¹ããæã¤ããã«ãããã¨ã ã
class Contact(db.Model): # User that owns this entry. owner = db.UserProperty() # Basic info. name = db.StringProperty() birth_day = db.DateProperty() # Address info. address = db.PostalAddressProperty() # Company info. company_title = db.StringProperty() company_name = db.StringProperty() company_description = db.StringProperty() company_address = db.PostalAddressProperty() # Group affiliation groups = db.ListProperty(db.Key)
ã°ã«ã¼ãã«ã¦ã¼ã¶ã¼ã追å ã»åé¤ãããã¨ã¯ãã¼ã®ãªã¹ããæä½ãããã¨ã«ãªãã
friends = Group.gql("WHERE name = 'friends'").get() mary = Contacts.gql("WHERE name = 'Mary'").get() if friends.key() not in mary.groups: mary.groups.append(friends.key()) mary.put()
ã°ã«ã¼ãã®ã¡ã³ãã¼å ¨å¡ãè¦ããã¨ãããç°¡åãªã¯ã¨ãªãå®è¡ããã ãã§ãããGroupã¨ã³ãã£ãã£ã«ãã«ãé¢æ°ãä½ãã¨ä¾¿å©ã ããã
class Group(db.Model): name = db.StringProperty() description = db.TextProperty() @property def members(self): return Contact.gql("WHERE groups = :1", self.key())
ãã®ããæ¹ã§å¤å¯¾å¤é¢é£ãå®è£ ããå ´åã¯å¶éãå°ããããã¾ããå©ç¨ã§ããã®ãKeyãªãã¸ã§ã¯ãã ããªã®ã§ããªã¹ããä¿æããã¦ããã³ã¬ã¯ã·ã§ã³ã®å¤ã¯æ示çã«æ¤ç´¢ãã¦ãããªãã¨ãããªããããã«éè¦ãªå¶éã¯ãListPropertyã«ãã¾ã大ããªãªã¹ããä¿æããã®ã¯é¿ããã»ããããã¨ãããã¨ã ãã¤ã¾ããªã¹ãã«å ¥ããã®ã¯é¢é£ã®ãã¡ããå°ãªãæ°ã«ãªãã¨æãããæ¹ã«ããã»ãããããä»åã®ä¾ã§ã¯Contactããªã¹ãã«å ¥ããããããã¯ä¸äººã®äººãä½ãã«ããããã®ã°ã«ã¼ãã«æå±ãããã¨ã¯ãªãããã ãã©ãã°ã«ã¼ãã¯ä½ç¾ãã®ã¡ã³ããæã¡ããã ããã ã
é¢é£ã¢ãã«
ã¦ã¼ã¶ã®ä¸äººããããã¯ã©ã¹ã®ã»ã¼ã«ã¹ã¦ã¼ãã³ã§ããä¼ç¤¾ã®ãã¼ã ã®äººãã¡ãç¥ã£ã¦ãã¨ãããã彼女ã¯åãä¼ç¤¾ã®æ å ±ãä½åº¦ãä½åº¦ãå ¥åããªããã°ãããªãã¦ã¨ã¦ãé¢åãããã¨æããç¹å®ã®ä¼ç¤¾ã®æ å ±ã¯ä¸åº¦ã ãå ¥åãã¦ããããã®äººãããã«é¢é£ä»ããããã«ã¯ã§ããªããã ãããï¼ããåç´ãªè©±ãªãåã«Contactã¨Companyã«ä¸å¯¾å¤ã®é¢é£ãæããã ãã§ãããã話ã¯ããç°¡åã§ããªãã彼女ã®é£çµ¡å ã®ä½äººãã¯å¥ç´ç¤¾å¡ã§è¤æ°ã®ä¼ç¤¾ã«æå±ãã¦ãããããããã§ç°ãªãè©æ¸ããæã£ã¦ããããã¦ã©ãããï¼
ããã§å¿ è¦ãªã®ã¯é¢é£èªèº«ã«ã¤ãã¦ãªã«ã追å ã®æ å ±ãæã¦ããããªå¤å¯¾å¤é¢é£ã ããã®ããã«ã¯é¢é£ã表ãã¢ãã«ã追å ããã°ãã:
class Contact(db.Model): # User that owns this entry. owner = db.UserProperty() # Basic info. name = db.StringProperty() birth_day = db.DateProperty() # Address info. address = db.PostalAddressProperty() # The original organization properties have been replaced by # an implicitly created property called 'companies'. # Group affiliation groups = db.ListProperty(db.Key) class Company(db.Model): name = db.StringProperty() description = db.StringProperty() company_address = db.PostalAddressProperty() class ContactCompany(db.Model): contact = db.ReferenceProperty(Contact, required=True, collection_name='companies') company = db.ReferenceProperty(Company, required=True, collection_name='contacts') title = db.StringProperty()
誰ãã«ä¼ç¤¾æ å ±ã追å ããããã°ContactCompanyã¤ã³ã¹ã¿ã³ã¹ãä½æããã°ããã
mary = Contacts.gql("name = 'Mary'").get() google = Company.gql("name = 'Google'").get() ContactCompany(contact=mary, company=google, title='Engineer').put()
ãã®æ¹æ³ã使ãã¨ãé¢é£ã«é¢ããæ å ±ãä¿æã§ããããã«ãªã£ãã ãã§ã¯ãªãããã¼ã®ãªã¹ããæã¤æ¹æ³ã¨æ¯ã¹ã¦ãããããããã®é¢é£ãå¦çã§ããã¨ããå©ç¹ãããããã ããé¢é£ããã©ãã¼ã¹ããã®ã«ããããããã®ãã¼ã¿ã¹ãã¢ã¸ã®ã¢ã¯ã»ã¹ãçºçãããã¨ã«ãªãã®ã§æ°ãã¤ããå¿ è¦ãããããã®æ¹å¼ã®å¤å¯¾å¤é¢é£ã使ãã®ã¯ãããæ¬å½ã«å¿ è¦ãªå ´åã ãã«ãã¦ãå®éã«å©ç¨ããå ´åãã¢ããªã±ã¼ã·ã§ã³ã®ããã©ã¼ãã³ã¹ã«ã¯æ°ãé ããã
çµè«
App Engineã§ã¯ç°¡åã«ãã¼ã¿ã¹ãã¢ã¨ã³ãã£ãã£éã®é¢é£ãä½æãã¦ãç¾å®ä¸çã®ã¢ã¤ãã¢ã表ç¾ã§ãããããä¸åã®ã¨ã³ãã£ãã£ãä¸å®æ°ã®æ å ±ãæã¤å¿ è¦ããããªãReferencePropertyã使ãããã»ãã®ã¤ã³ã¹ã¿ã³ã¹ã¨ãäºãã«å ±æãããããããã®ç°ãªããªãã¸ã§ã¯ããå¿ è¦ãªããã¼ã®ãªã¹ãã使ããããã®äºã¤ã®ã¢ããã¼ãã§ã¢ããªã±ã¼ã·ã§ã³ã§ä½¿ç¨ããã¢ãã«ã®ã»ã¨ãã©ãã«ãã¼ã§ãããã¨ã«æ°ã¥ãã ããã