Python 在dhangoforms中隐藏从模型生成的字段并将其保存在数据库中
1] 我有以下型号:Python 在dhangoforms中隐藏从模型生成的字段并将其保存在数据库中,python,django,google-app-engine,Python,Django,Google App Engine,1] 我有以下型号: class UserReportedData(db.Model): #country selected by the user, this will also populate the drop down list on the html page country = db.StringProperty(default='Afghanistan',choices=['Afghanistan']) #city selected by the user city
class UserReportedData(db.Model):
#country selected by the user, this will also populate the drop down list on the html page
country = db.StringProperty(default='Afghanistan',choices=['Afghanistan'])
#city selected by the user
city = db.StringProperty()
user_reported_boolean = db.BooleanProperty() # value that needs to be hidden from the display
date = db.DateTimeProperty(auto_now_add=True)
class UserReportedDataForm(djangoforms.ModelForm):
class Meta:
model = UserReportedData
2] 决定布尔值“user\u reported\u boolean”的html代码如下所示
'<input type="submit" name="report_up" value= "Report Up">'
'<input type="submit" name="report_down" value= "Report Down">'
class UserReporting(webapp.RequestHandler):
def post(self):
#get the data that the user put in the django form UserReportForm
data = UserReportedDataForm(data=self.request.POST)
#need to find whether user hit the button with the name "report_up" or "report_down"
# this is not working either
if 'report_up' in self.request.POST:
data.user_reported_boolean = True
elif 'report_down' in self.request.POST:
data.user_reported_boolean = False
if data.is_valid():
# Save the data, and redirect to the view page
entity = data.save(commit=False)
entity.put()
self.redirect('/')
问题:
1] 如何隐藏字段“user\u reported\u boolean”不显示在html表单上
2] 如何在数据库中保存此字段“user\u reported\u boolean”1:从模型表单中排除此字段。
2:在未保存的模型实例中,您可以通过commit=False
您希望字段处于表单中,但在显示时隐藏,还是不希望它处于表单中?@anand,是的,我希望字段“user\u reported\u boolean”出现在模型中,但不希望显示。@anand,我尝试了下面@Yuji Tomita给出的响应,并成功地从html页面中隐藏了模型中的一个字段。谢谢@Yuji Tomita,这很有效。再次感谢您的详细回复
class MyModelForm(ModelForm):
class Meta:
model = Foo
exclude = ('myfield',)
entity = data.save(commit=False)
entity.user_reported_boolean = True # False, whatever.
entity.save()