2013-07-15 22 views
0

我有一個候選模型之一獲取關鍵的領域,而不是完整的密鑰

class Candidate(ndb.Model): 
    name = ndb.StringProperty() 
    phone = ndb.StringProperty() 
    location_name = ndb.StringProperty() 
    state = ndb.KeyProperty(kind=Setting) 

型號叫做提交國家

class SubmissionState(ndb.Model): 
    name = ndb.StringProperty(required=True) 
    description = ndb.TextProperty() 

模式設置,用來根據類別來處理設置,用戶選擇。所有這些工作正常。

這是views.py。還有一個post方法,但我不確定這是否有必要。

class EditSubmission(webapp2.RedirectHandler): 

    def get(self): 
     candidateid = self.request.path.split('/')[-1] 
     candidate = Candidate._get_by_id(int(candidateid)) 
     template_values = {'candidate': candidate, } 
     path = os.path.join(os.path.dirname(__file__), '../templates/edit_submission.html') 
     self.response.write(template.render(path, template_values)) 

這裏是模板的一部分:

<tr> 
     <th><label for="id_state">Submission State:</label></th> 
     <td><input id="id_state" type="text" name="state" value="{{ candidate.state.name }}"/></td> 
    </tr> 

如果我做{{ candidate.state }}我拿到鑰匙和所有的領域,但我不能因爲某些原因只得到名稱。我會怎麼做?

謝謝。

回答

0

candidate.state只是指向提交狀態的關鍵。您的應用需要發出另一個請求到數據存儲,以獲取SubmissionState實體,像這樣:

state_name = candidate.state.get().name 

您可以在您的處理程序執行得到(),然後添加STATE_NAME到您的模板值,所以它的可用內模板。

+0

謝謝!這沒有竅門:) –

相關問題