2012-04-15 65 views
4

我回到了AppEngine開發中。 我很喜歡Datastorage。AppEngine id/key/from_path相當混亂我應該怎麼使用

我習慣了「IDS」,建立網站,如「/視圖?ID = 322345」鏈接讓我這個ID瞧的留言條目。

但在AppEngine中,我總是看到調用的db.Key.from_path()方法以及其他諸如「祖先」和「父母」之類的東西。

在我的項目中,我有像User-> Phonenumbers這樣的簡單關係,並且我想通過一個GET url來檢查每個用戶,並在其中添加類似id的內容。 我應該使用用戶模型密鑰()嗎?或者如何在AppEngine中實現?

目前,如果我想也就是一個PhoneNumber添加到我寫了下面的用戶:

class JoinHandler(webapp2.RequestHandler): 
    def get(self): 
     user_key = self.request.get('rlm') 
     fon_number = PhoneNumber() 
     fon_number.country = "deDE" 
     fon_number.user = db.Key(user_key) 
     fon_number.put() 
     self.redirect("/") 
+1

類似於你的問題:http://stackoverflow.com/questions/10156129/what-should-i-store-as-my-index-in-client-code – alex 2012-04-15 16:44:38

回答

9

如果您使用的是數字ID由數據存儲區自動分配,當你put一個實體,就可以然後用模型的方法get_by_id存取實體:

class User(db.Model): 
    name = db.StringProperty() 
    # etc. 

def AddUser(webapp2.RequestHandler): 
    def get(self): 
     newUser = User() 
     newUser.name = self.request.get('name') 
     # etc. assign all model properties 
     newUser.put() 
     # now, newUser.Key().id() contains the auto-assigned numeric id. 
     # we can pass this value to our template and get it back via 
     # a form paramter for subsequent operations. 

def ShowUser(webapp2.RequestHandler): 
    def get(self): 
     user_to_show_id = self.request.get('id') 
     user_to_show = User.get_by_id(user_to_show_id) 
     if user_to_show is not None: 
      # populate the template parameters with the users's data 
      # and render the template 
     else: 
      # The requested user does not exist, so 
      # render an error message/page. 

現在,如果你也想存儲屬於特定用戶的電話號碼實體,你讓他們保存,使得他們的父母是用戶實體。這將它們放在用戶的實體組中,並且實體組中的實體可以比與父母一起存儲時更快地被查詢。

比方說,任何用戶都可以有與之相關Phonenumber來實體中的任何給定數量:

class Phonenumber(db.Model): 
    number_type = db.StringProperty() 
    number = db.StringProperty() 

我們將添加一個方法到User類:

def AddNumber(self, new_number_type, new_number): 
    new_number = Phonenumber(parent=self, number_type=new_number_type, number=new_number) 
    # In this case, self is the User entity, and by saying parent=self, we're putting 
    # this Phonenumber entity into User's entity group. 
    new_number.put() 

def GetPhoneNumber(self): 
    # Now, getting all of User's phone numbers is a simple as a query with an 
    # Ancestor clause: 
    users_numbers = Phonenumber.all().ancestor(self).fetch(1000) 

    return users_numbers 

顯然,這些類天真無邪,直截了當,但我希望他們能幫助你理解身份證和祖先的關係。通常情況下,您不必使用Key()手動創建密鑰。除非你真的需要,否則不要走這條路。然而,如何理解鍵的作用是有利於真正理解AppEngine上,所以潛水和實驗,如果你喜歡它的感覺。

我覺得那些是你的兩個主要問題,是嗎?如果你有其他人,請繼續留言,我會編輯我的答案。

+1

你不需要將user_to_show_id投入int嗎?它不適合我,如果我不施放它...... – 2013-09-16 04:34:59

+0

很可能是真的,Fabien。感謝您指出了這一點。 – 2013-09-16 15:48:10

+0

謝謝!我想知道我是否做錯了什麼。我是新來的。 – 2013-09-16 16:50:58

2

至於你什麼db.Key.from_path()會做題,檢查Key Class docs,你會看到的第一個參數是from_path()數據存儲對象的那種你會被檢索。

參數數量被列出象這樣:

Key.from_path(kind, id_or_name, parent=None, namespace=None)

在我的基本的博客的應用程序之一,我有以下行:

key = db.Key.from_path('BlogPost', int(post_id), parent=blog_key())

這是尋找BlogPost類型的對象,隨着post_id的ID,並等於blog_key()方法的返回值的父。

blog_key()被定義爲:

def blog_key(name = 'default'): 
return db.Key.from_path('blogs', name) 

我會跟你坦白說:我還沒有想通了這部分未到(udacity cs253逐字複製),但我認爲它的建立是祖先值在所有BlogPost對象的數據存儲區中。

我只用GAE工作了幾個星期,通過cs253過程中工作,所以我會很感激有這方面的澄清,或更清楚地解釋說,「發生了什麼」