AppEngine - 如何使用键以一对一关系将对象连接到另一个对象

AppEngine - How to join object to another in one-one relationship using key

我的模型中有两种实体(咨询和评估):

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()

首先创建一个咨询并将其放入数据存储区。可以通过 consults/view-consult 页面查看此咨询。 url 嵌入了咨询的密钥:

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

在此页面上,用户可以单击 link 说 "Add Assessment",这会将他们发送到 consults/insert-assessment 页面,并像以前一样嵌入密钥:

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()

对于 1:1 关系中的实体,您可以使用相同的密钥 ID,这除了其他优点外,还可以轻松实现从一个实体到另一个实体的交叉引用。

参见,例如:

  • 中的第二个示例

对于您的代码示例:

  • /templates/view-consult.html 的某处,您会将 consult 的密钥字符串表示形式 connect_key_string 甚至其密钥 ID connect_id 传递给 post 表单,这样信息就可以在 post() 请求

  • 中使用
  • post() 方法中,您将直接使用该 ID 或恢复 connect 实体的密钥(与您的其他 post ) 然后获取密钥的 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()