1

I have a Subject model and a CustomUser model. During registration users can select multiple subjects. The following is my code in forms.

forms.py

class SignUpForm(forms.Form):
    ...
    subjects = forms.ModelMultipleChoiceField(label="Subjects",
                                         widget=forms.CheckboxSelectMultiple,
                                         queryset=Subject.objects.all())

What can I do in views.py to save this data? The usual method of cleaning the data and then using the save method doesn't work unfortunately. Scarily, similar questions have very little to no answers in SO.

MiniGunnR
  • 5,590
  • 8
  • 42
  • 66

1 Answers1

0

Nevermind. I found it.

if password == password2:
                u = CustomUser.objects.create_user(username, email, password, first_name=fname, last_name=lname, dob=year+'-'+month+'-'+day)
                u.subjects = subjects
                u.save

I made the mistake of trying to squeeze in the subjects in the create_user method with all the other variables.

MiniGunnR
  • 5,590
  • 8
  • 42
  • 66
  • Can you please add a bit more of details how to implement in views.py, and how the selected subjects store in DB. I'm facing the same issue and cannot find any detailed example for processing and storing into DB the ``ModelMultipleChoiceField`` with ``CheckboxSelectMultiple`` – arminrock Aug 05 '16 at 12:50