2

I have a UserProfile where the User chose a language. Users should have the possibility to chose more than one language so i tried to use a ManyToManyField like so:

class Choices(models.Model):
    languages = models.CharField(choices=settings.LANGUAGES, max_length=3)

def __unicode__(self):
    return self.languages

class UserProfile(models.Model):
    user = models.OneToOneField(User, null=True)
    language = models.ManyToManyField(Choices)

in my form.py i did:

class UserProfileForm(forms.ModelForm):
language = forms.MultipleChoiceField(required=False, choices=settings.LANGUAGES)
class Meta:
    model = UserProfile
    fields=[
        'Pictures',
         .....
     ]

The problem is: The choices are shown in the template but in the admin the ManyToManyField is empty. So the Form works but the Model does not. Can somebody tell me why no Options are displayed? Where is the mistake at the M2M?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Marla
  • 101
  • 1
  • 11
  • Have you created (and saved) instances of the Choices model? – lufte May 15 '17 at 19:57
  • What is your error?? Can you post the view please?? – zaidfazil May 16 '17 at 01:52
  • No i did not. @ lufte. That seems to be the problem. I solved this by using a MultipleSelectField. Thanks anyway for the Effort. @Fazil The error was that I had no Options to select in the Admin the M2M was just empty... – Marla May 16 '17 at 11:48

0 Answers0