按照 Neomodel 文档中提供的示例,我无法在表单中显示选项。
class Person(StructuredNode):
SEXES = {'F': 'Female', 'M': 'Male', 'O': 'Other'}
sex = StringProperty(required=True, choices=SEXES)
通过使用此示例,我在执行 Django 时遇到错误。
packages/django_neomodel/__init__.py", line 122, in get_choices
for choice, __ in choices:
ValueError: not enough values to unpack (expected 2, got 1)
修改 SEXES 并指定 'FF'、'MM' 等将允许服务器运行,但值显示为
<option value="F">F</option>
<option value="M">M</option>
<option value="O">O</option>
我在模型中使用了以下代码。
COUNTRY_CODE1=[('AF', 'Afghanistan (+93)'), ('AL', 'Albania (+355)'), ('DZ', 'Algeria')]
...
ind_nationality = StringProperty(max_length=2,choices=COUNTRY_CODE1,label='Nationality')
当它在浏览器中填充时,将生成以下内容。
<option value="A">F</option>
<option value="A">L</option>
<option value="D">Z</option>
模型.py
class Person(DjangoNode):
uid_person = UniqueIdProperty()
ind_name = StringProperty(max_length=100 , label='Enter your First Name')
ind_last_name = StringProperty(max_length=100,null=True, label='Last Name')
ind_nationality = StringProperty(max_length=2,choices=COUNTRY_CODE1,label='Nationality')
表格.py
class PersonRegForm (ModelForm):
class Meta:
model=Person
fields = ['ind_name','ind_last_name', 'ind_nationality']
widgets = {
'ind_name' : forms.TextInput(attrs={'size':50}),
'ind_last_name' : forms.TextInput(attrs={'size':50}),
'ind_nationality' : forms.Select(),
}
app_label = 'reg'
class PersonRegView(generic.FormView):
template_name='reg/personreg.html'
form_class=PersonRegForm
success_url = 'index'
我期待以下结果
<option value="AF"></option>