0

Following the example provided in Neomodel documentation I am not able to display choices in my form.

class Person(StructuredNode):
    SEXES = {'F': 'Female', 'M': 'Male', 'O': 'Other'}
    sex = StringProperty(required=True, choices=SEXES)

by using this example I get an error when Django is executed.

packages/django_neomodel/__init__.py", line 122, in get_choices
    for choice, __ in choices:
ValueError: not enough values to unpack (expected 2, got 1)

Modifying the SEXES and specifying 'FF', 'MM' etc will allow the server to run but values appear as

<option value="F">F</option>
<option value="M">M</option>
<option value="O">O</option>

I have used the following code in models.

COUNTRY_CODE1=[('AF', 'Afghanistan (+93)'), ('AL', 'Albania (+355)'), ('DZ', 'Algeria')]
...
ind_nationality = StringProperty(max_length=2,choices=COUNTRY_CODE1,label='Nationality')

When this is populated in a browser the following is generated.

<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'

I am expecting the following result

<option value="AF"></option>
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895

1 Answers1

0

When I've done choice fields I use a field called ChoiceField, which accepts a list or tuple of tuples.

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

I'm not sure how Neomodel works but this is how I've set it up in my django project.