0

I'm trying to change the default multi select from a many to many field in django to checkboxes. I managed to display the checkboxes and everything but in the browser I only get the second character of each field value, while I get the full list when I type the same command in the django shell.

Example:

When I type:

from myapp.models import Interest

obj_list = Interest.objects.all()
x = [obj.interest_mame for obj in obj_list]
x

I get the expected result which is (for the first few values)

[u'Architektur', u'Handwerken', u'Ausgehen', u'manymorestringshere']

However when I open my registration form in the Browser, I get only the second letter of each string.

Would be r, a, u, a in this case next to a checkbox.

My file looks like this:

from django.forms import ModelForm
from myapp.models import Person, Interest

from django import forms
from django.forms.extras.widgets import SelectDateWidget


class PersonForm(ModelForm):
    year_range = range(1996, 1920, -1)
    person_birthdate = forms.DateField(widget=SelectDateWidget(years=year_range))
    obj_list = Interest.objects.all()
    person_interests = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=[obj.interest_name for obj in obj_list])
    class Meta:
        model = Person

I thought about maybe it would be the u in front of every string, but I read that this would only show it is an unicode string in another question.

I would be very happy if one of you guys could explain to me why I get this weird display bug.

Community
  • 1
  • 1
SAF
  • 317
  • 4
  • 20

1 Answers1

1

Choices need to be a list of tuples (value, display_value), as shown in the official documentation. If you want the display value to to be the same as the submitted value, you could do choices=[(obj.interest_name, obj.interest_name) for obj in obj_list], for example.

sk1p
  • 6,645
  • 30
  • 35