2013-03-19 30 views
1

我正在使用gae-boilerplate(webapp2和jinja2)。我的模型如下所示:如何通過谷歌應用引擎引用實體使用NDB

class Location(ndb.Model): 
    x = ndb.FloatProperty() 
    y = ndb.FloatProperty() 

class Criterium(polymodel.PolyModel): 
    name = ndb.StringProperty(required=True) 
    user = ndb.KeyProperty(kind='User') 
    priority = ndb.IntegerProperty(required=True) 

class Work(Criterium): 
    location = ndb.StructuredProperty(Location) 

class Friend(Criterium): 
    location = ndb.StructuredProperty(Location) 

我有一個來自Criterium模型的所有記錄的表。我想在那裏添加一個刪除鏈接,但我不知道爲了引用特定的實體而使用它來傳遞什麼參數。我的處理程序是這樣的:

def get(self): 
    criteria = Work.query().order(-Criterium.priority, Criterium.name) 
    self.view.list_columns = [('name', 'Name'), 
         ('priority', 'Priority'), 
         ('className', 'Type')] 
    self.view.criteria = criteria 
    self.view.count = criteria.count() 
    params={} 
    self.render_template('list.html', **params) 

似乎是一個簡單的問題,卻一直在掙扎了一整天...提前感謝!

+0

單數標準是'標準',而不是'標準'。 – 2013-03-19 16:27:29

+0

謝謝。不知何故,我不喜歡「標準」這個詞。 – 2013-03-19 17:35:19

回答

1

那麼id的對象呢?
那就是Criterium模型中該對象的唯一標識符。

上循環的條件的實體時:

for criterum in criteria: 
    criterum.key.id() 
+0

謝謝,這似乎是工作。 我現在在我的html中使用這個: 這是安全嗎? – 2013-03-19 16:24:08

+0

不知道它有多安全,但這是我如何刪除訂單項 – 2013-03-19 17:05:13

+0

我想最好將密鑰傳回工作 - 刪除? – 2013-03-19 17:09:10

0

見下文

從google.appengine.ext進口NDB例如

類用戶(ndb.Model):

created = ndb.DateTimeProperty(auto_now_add = True) 
firstName = ndb.StringProperty(required = True) 
lastName = ndb.StringProperty(required = True) 
email = ndb.StringProperty(required = True) 
pwHash = ndb.StringProperty(required = True) 

@classmethod 
def byEmail(cls, email): 
    u = cls.query(cls.email == email).get() 
    return u 

@classmethod 
def register(cls, firstname, lastname, email, password): 
    pwhash = utils.makePwHash(email, password) 
    return User(firstName=firstname, 
     lastName=lastname, 
     email=email, 
     pwHash=pwhash) 

@classmethod 
def login(cls, email, password): 
    u = User.byEmail(email) 
    if u and utils.validPW(email, password, u.pwHash): 
     return u 
相關問題