1

我有兩種實體的在我的模型(諮詢誰和評估):AppEngine上 - 如何使用加入對象到另一個一一對應的關係,關鍵

class Consults(ndb.Model): 

# Basic Consult Info (To get started storing a consult in the datastore) 

    # Timestamp consult submitted to datastore 
    consult_created = ndb.DateTimeProperty(auto_now_add=True) 
    # Consult booking date 
    consult_date = ndb.StringProperty() 
    # Consult booking time 
    consult_time = ndb.StringProperty() 
    # Provider booking the consult 
    consult_user = ndb.StringProperty() 
    # Consult status: (Pending, Completed, Cancelled) 
    consult_status = ndb.StringProperty(choices=('Pending','Completed','Cancelled'),default='Pending') 

# Patient Info 

    # The patient's first name 
    patient_first = ndb.StringProperty() 
    # The patient's last name 
    patient_last = ndb.StringProperty() 
    # The patient's email address 
    patient_phone = ndb.StringProperty() 
    # The patient's phone number 
    patient_email = ndb.StringProperty() 
    # The patient's age in years 
    patient_age = ndb.IntegerProperty() 
    # Does the patient agree to emails from JW? 
    patient_optin = ndb.BooleanProperty() 

# Clinical Info 

    # Does the patient use an orthodic? 
    clin_ortho = ndb.BooleanProperty() 
    # Foot type:(Over Pronated, Moderatly Pronated, Neturtal, Supinated, Orthosis) 
    clin_type = ndb.StringProperty(choices=('Over Pronated','Moderately Pronated','Neutral','Supinated','Orthosis')) 
    # The measured leangth of the foot 
    clin_length = ndb.IntegerProperty() 
    # The measured width of the foot  
    clin_width = ndb.IntegerProperty()       
    # Provider notes - previous injury history 
    clin_inj = ndb.TextProperty()        
    # Provider notes - recommendations on footware 
    clin_recom = ndb.TextProperty()      
    # Regular physical activity [1] 
    clin_activ1 = ndb.StringProperty()       
    # Activity frequency: (daily, weekly) [1] 
    clin_freq1 = ndb.StringProperty(choices=('Daily','Weekly'))         
    # Regular physical activity [2] 
    clin_activ2 = ndb.StringProperty()       
    # Activity frequency: (daily, weekly) [2] 
    clin_freq2 = ndb.StringProperty(choices=('Daily','Weekly'))        
    # Regular physical activity [3] 
    clin_activ3 = ndb.StringProperty()       
    # Activity frequency: (daily, weekly) [3] 
    clin_freq3 = ndb.StringProperty(choices=('Daily','Weekly'))     

class Assessments(ndb.Model): 

# JW Recommendations 

    # JW consultant requested - can be overidden by consultant 
    assess_consultant = ndb.StringProperty() 
    # Consultant notes - general recommendation notes 
    assess_notes = ndb.TextProperty() 

# Recommended Shoe [1] 

    # Product ID/link 
    assess_pid1 = ndb.StringProperty()           
    # Category 
    assess_category1 = ndb.StringProperty()        
    # Brand 
    assess_brand1 = ndb.StringProperty()       
    # Model 
    assess_model1 = ndb.StringProperty()       
    # Size 
    assess_size1 = ndb.StringProperty()       
    # Width 
    assess_width1 = ndb.StringProperty()        

# Recommended Shoe [2] 

    # Product ID/link 
    assess_pid2 = ndb.StringProperty() 
    # Category 
    assess_category2 = ndb.StringProperty() 
    # Brand 
    assess_brand2 = ndb.StringProperty() 
    # Model 
    assess_model2 = ndb.StringProperty() 
    # Size 
    assess_size2 = ndb.StringProperty() 
    # Width 
    assess_width2 = ndb.StringProperty() 

# Recommended Shoe [3] 

    # Product ID/link 
    assess_pid3 = ndb.StringProperty()         
    # Category 
    assess_category3 = ndb.StringProperty()        
    # Brand 
    assess_brand3 = ndb.StringProperty()       
    # Model 
    assess_model3 = ndb.StringProperty()       
    # Size 
    assess_size3 = ndb.StringProperty()       
    # Width 
    assess_width3 = ndb.StringProperty() 

首先一個諮詢創建並放置到數據存儲。該諮詢可以通過諮詢/查看諮詢頁面查看。該網址中有協商的主要嵌入式:

http://localhost:8080/consults/view-consult?key=aghkZXZ-Tm9uZXIVCxIIQ29uc3VsdHMYgICAgIDIkwkM 

在這個頁面上,用戶可以點擊一個鏈接說:「加入評估」,將它們發送到諮詢誰/插入評估頁,還嵌入前的關鍵:

http://localhost:8080/schedule/insert-assessment?key=aghkZXZ-Tm9uZXIVCxIIQ29uc3VsdHMYgICAgIDIkwkM 

問題是如何在用戶點擊提交併發佈評估對象(每次諮詢只有一個評估附加)時以一對一的關係連接這些對象。

你我需要添加一些東西到我的評估模型?

此外,我希望在視圖諮詢頁面上顯示兩個對象的參數。

編輯

class ViewConsultPage(webapp2.RequestHandler): 
    def get(self): 
     consult = ndb.Key(urlsafe=self.request.get('key')).get() 
     template = JINJA_ENVIRONMENT.get_template('/templates/view-consult.html') 
     template_values = { 
     'consult': consult 
     } 
     self.response.out.write(template.render(template_values)) 
    def post(self): 
     jw_consultant = self.request.get("jw_consultant") 
     jw_notes = self.request.get("jw_notes") 
     pid1 = self.request.get("pid1") 
     cat1 = self.request.get("cat1") 
     brand1 = self.request.get("brand1") 
     model1 = self.request.get("model1") 
     size1 = self.request.get("size1") 
     width1 = self.request.get("width1") 
     pid2 = self.request.get("pid2") 
     cat2 = self.request.get("cat2") 
     brand2 = self.request.get("brand2") 
     model2 = self.request.get("model2") 
     size2 = self.request.get("size2") 
     width2 = self.request.get("width2") 
     pid3 = self.request.get("pid3") 
     cat3 = self.request.get("cat3") 
     brand3 = self.request.get("brand3") 
     model3 = self.request.get("model3") 
     size3 = self.request.get("size3") 
     width3 = self.request.get("width3") 
     assessment = Assessments(id=consult.key.id(), 
           assess_consultant=jw_consultant, 
           assess_notes=jw_notes, 
           assess_pid1=pid1, 
           assess_category1=cat1, 
           assess_brand1=brand1, 
           assess_model1=model1, 
           assess_size1=size1, 
           assess_width1=width1, 
           assess_pid2=pid2, 
           assess_category2=cat2, 
           assess_brand2=brand2, 
           assess_model2=model2, 
           assess_size2=size2, 
           assess_width2=width2, 
           assess_pid3=pid3, 
           assess_category3=cat3, 
           assess_brand3=brand3, 
           assess_model3=model3, 
           assess_size3=size3, 
           assess_width3=width3) 
     assessment.put() 
+0

附註:您可能希望將您的代碼片段修剪爲MCVE:http://stackoverflow.com/help/mcve –

回答

2

對於在1實體:可以使用相同的密鑰ID,其中,除了其它優點之外,使得從一個實體向另一個微風交叉引用1的關係。

見,例如:

對於您的代碼示例:

  • 某處/templates/view-consult.html你會通過consult「 s密鑰字符串repres entation connect_key_string甚至它的密鑰ID connect_id到後期的形式,使信息然後在post()要求

  • post()方法,你會直接使用該ID或恢復connect實體的鍵(如在其他可用發佈Linking to entity from list),然後拿到鑰匙的ID:

    connect_id = int(self.request.get("connect_id")) 
    assessment = Assessment(id=connect_id) 
    assessment.put() 
    

    connect_key = ndb.Key(urlsafe=self.request.get('connect_key_string')) 
    assessment = Assessment(id=connect_key.id()) 
    assessment.put() 
    
+0

感謝Dan,試圖瞭解如何將「重新使用實體」應用於我的特定環境。是否像我需要兩個使用KeyProperty以某種方式添加屬性到每個模型? – TimothyAURA

+0

創建評估實體時,請指定其KeyID與相應的Consult的KeyID相同:評估=評估(id = consult.key.id())' '評估.put()' –

+0

偉大 - 我會給那一試。 – TimothyAURA