我有一个从基础表填充的下拉列表,但是在渲染时,下拉列表中没有选择任何值,或者显示“-------”。不用说“------”不对应任何表行。
这是代码:
class CompanyCategory(db.Model):
categoryname = db.StringProperty(required=False)
def __unicode__(self):
return u'%s' % (self.categoryname)
class SelectCategoryForm(djangoforms.ModelForm):
class Meta:
model = SelectCat
代码在 html 中使用此代码呈现
{ form.companycategory }}
这是错误:
Traceback (most recent call last):
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 703, in __call__
handler.post(*groups)
File "/base/data/home/apps/s~biomapit/1.359892355004556500/showcompanies.py", line 163, in post
mycategoryid = [form_requirements.clean_data['companycategory'].key().id()]
AttributeError: 'NoneType' object has no attribute 'key'
如何在呈现下拉菜单时更改默认值或删除“-----”。如果我将categoryname = db.StringProperty(required=False)
行更改为categoryname = db.StringProperty(required=True)
.
作为一个响应后的额外尝试,我尝试过:
class SelectCategoryForm(djangoforms.ModelForm):
class Meta:
model = SelectCat
def __init__(self, *args, **kwargs):
super(SelectCategoryForm, self).__init__(*args, **kwargs)
modelchoicefields = [field for field_name, field in self.fields.iteritems() if isinstance(field, forms.ModelChoiceField)]
for field in modelchoicefields:
field.empty_label = None
无济于事-我仍然得到“-----”。