我正在尝试在models.py国家/地区中执行选择选项,以便用户可以选择,但无法正确显示

时间:2019-02-06 11:33:31

标签: django neo4j neomodel

按照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>

models.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')

forms.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>

1 个答案:

答案 0 :(得分:0)

完成选择字段后,我使用名为ChoiceField的字段,该字段接受元组列表或元组。

class Person(StructuredNode):
    SEXES = (
        ('F', 'Female'), 
        ('M', 'Male'), 
        ('O', 'Other'),
    )
    sex = models.ChoiceField(required=True, choices=SEXES)

https://docs.djangoproject.com/en/2.1/ref/forms/fields/#choicefield https://docs.djangoproject.com/en/2.1/ref/models/fields/#field-choices

我不确定Neomodel是如何工作的,但这就是我在django项目中进行设置的方式。