Riko
Riko

Reputation: 131

Django. How to show in form ManyToMany field as a Select (dropdown list)

I have a ManyToMany relation User->User_Towns->Towns...

towns = models.ManyToManyField("Town", blank=False,verbose_name='Town',related_name="user_set", related_query_name="user")

...and in SignForm a want to show a DropDownList instead MultipleChoice. If in a form.py I write some like:

  fields = ('email', 'first_name', 'last_name', 'middle_name','towns', 'phone',)
    widgets = {
        'towns': forms.Select(),
    }

I get a DropDownList but if I choose a town and submit the form it returns me an error:

[Enter a list of values]

Can you help me with this issue?

Upvotes: 2

Views: 1457

Answers (1)

ruddra
ruddra

Reputation: 51938

You can put this method in the form.py:

class YourForm(forms.ModelForm):
   ...
   def clean_towns(self):
      return [self.cleaned_data['towns']]

Upvotes: 3

Related Questions