Python Modelform不打印一对多关系的字段
摘要: 我在模型之间有1到多个层次关系 国家(1)->城市(多个) 城市(1)-->状态(多个) 我有一个表单,它应该打印属于所有这些模型的字段,但是当我打印时,我只看到“city”字段,它也显示为下拉列表,而不是文本框。我试图寻找这个问题,但没有找到解决办法 代码摘录:Python Modelform不打印一对多关系的字段,python,django,google-app-engine,django-forms,Python,Django,Google App Engine,Django Forms,摘要: 我在模型之间有1到多个层次关系 国家(1)->城市(多个) 城市(1)-->状态(多个) 我有一个表单,它应该打印属于所有这些模型的字段,但是当我打印时,我只看到“city”字段,它也显示为下拉列表,而不是文本框。我试图寻找这个问题,但没有找到解决办法 代码摘录: from google.appengine.ext import db from google.appengine.ext.db import djangoforms class UserReportedCountry(d
from google.appengine.ext import db
from google.appengine.ext.db import djangoforms
class UserReportedCountry(db.Model):
#country selected by the user
country_name = db.StringProperty( required=True,
choices=['Afghanistan','Aring land Islands']
)
class UserReportedCity(db.Model):
country = db.ReferenceProperty(UserReportedCountry, collection_name='cities')
city_name = db.StringProperty(required=True)
class UserReportedStatus(db.Model):
city = db.ReferenceProperty(UserReportedCity, collection_name='statuses')
status = db.BooleanProperty(required=True)
date_time = db.DateTimeProperty(auto_now_add=True)
class UserReportedDataForm(djangoforms.ModelForm):
class Meta:
model = UserReportedStatus
exclude = ('status’ )
谢谢
[编辑#1]
我偶然发现了这篇文章()并遵循了提交者在页面上打印表单时使用的方法
A] 现在模型类中的窗体
class UserCountryForm(djangoforms.ModelForm):
class Meta:
model = UserReportedCountry
class UserCityForm(djangoforms.ModelForm):
class Meta:
model = UserReportedCity
exclude = ('country', )
class UserStatusForm(djangoforms.ModelForm):
class Meta:
model = UserReportedStatus
#hiding the site_is_up property
exclude = ('site_is_up', 'city' )
B] 打印这些表单的方法:
def print_user_reporting_form(self):
self.response_variable.out.write('<div id="userDataForm">'
'<form method="POST" '
'action="/UserReporting">'
'<table>' )
#method call to print the pre-populated user form with users country and city value
self.response_variable.out.write (UserCountryForm())
self.response_variable.out.write (UserCityForm())
self.response_variable.out.write(UserStatusForm())
self.response_variable.out.write ( '</table>'
'<input type="submit" name="report_up" value= "Report Up">'
'<input type="submit" name="report_down" value= "Report Down">'
'</form>'
'</div>')
def打印用户报告表(self):
self.response_变量.out.write(“”)
''
'' )
#方法调用以打印带有用户国家/地区和城市值的预填充用户表单
self.response\u variable.out.write(UserCountryForm())
self.response_variable.out.write(UserCityForm())
self.response\u变量.out.write(UserStatusForm())
self.response_variable.out.write(“”)
''
''
''
'')
谢谢,您的表单输出正确 城市和身份之间存在一对多关系
country
__ __ __ __ __ __ __ __ | __ __ __ __ __ __ __
/ | | \
city city city city
__ _|_ __ __ _|_ __ __ _|_ __ __ _|_ __
| | | | | | | | | | | | | | | |
s s s s s s s s s s s s s s s s
您有一个表单正在创建单个状态和城市之间的链接,您可以重复执行此操作以创建一个城市到多个状态的关系
下拉列表询问您希望将哪个城市与您的身份关联
您明确排除了状态字段并注意
注
按照目前的实施,设置
立即自动或立即自动添加到真实遗嘱
使该字段具有可编辑性=False
空白=真集
感谢@kriegar的快速回复。“UserReportedDataForm”中的“status”是故意排除的,因为我不希望该字段向用户公开,html表单中有一个逻辑,有两个按钮,根据选择的按钮,该标志将设置为true或false。我也不想把“日期和时间”暴露给用户。我想把“国家名”和“城市名”印在表格上。从我的模型声明中可以看到,“国家名称”应该是一个下拉列表,“城市名称”应该是一个文本框。有没有办法做到这一点?你是说城市名称字段的文本输入?还是一个文本区域?我想我明白你的要求了。您需要基于用户选择的国家/地区选择动态填充的城市名称字段。如果没有ajax,这是不可能的。我访问了link()并遵循了用户用于打印一对多模型表单的方法。你能检查一下我主要提交的内容中的[EDIT#1],看看我做得是否正确吗?