我有兩種實體的在我的模型(諮詢誰和評估):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()
附註:您可能希望將您的代碼片段修剪爲MCVE:http://stackoverflow.com/help/mcve –