Registering a user with BooleanFields

Hi Everyone,

I have created a my own custom user model:
Screenshot from 2020-04-06 18-49-16
and my registering forms are:

 class RegistrationForm(UserCreationForm):
     class Meta:
          model = Account
          fields = ("username", "password1", "password2")

class RegistrationForm2(UserCreationForm):

   guardianSource = forms.BooleanField(initial=True, required=False)
   bbcSource = forms.BooleanField(initial=False, required=False)
   independentSource = forms.BooleanField(initial=False, required=False)

   categoryCoronaVirus = forms.BooleanField(initial=True, required=False)
   categoryPolitics = forms.BooleanField(initial=False, required=False)
   categorySport = forms.BooleanField(initial=False, required=False)
   class Meta:
    model = Account
    fields = ("guardianSource", "bbcSource", "independentSource", "categoryCoronaVirus",                                          "categoryPolitics", "categorySport")

On registering a user I have 2 views, first it renders a form with the username and password, and on submitting it registers the user and redirects to another form with 6 booleanFields that I want to save into the database for the registered user:

def registration(request):
   context = {}
   if request.POST:
          form = RegistrationForm(request.POST)
          if form.is_valid():
                 form.save()
                 username = form.cleaned_data.get('username')
                 raw_password = form.cleaned_data.get('password1')
                 account = authenticate(username=username, password=raw_password)
                 login(request, account)
                 return redirect('register2') #redirect to 2nd form
         else:
             context['registration_form'] = form
    else: # GET request
          form = RegistrationForm()
          context['registration_form'] = form
    return render(request, 'accounts/register.html', context)

def registration2(request):
    context = {}
    if request.POST:
         form = RegistrationForm2(request.POST)
         if form.is_valid():
                form.save()
                return redirect('home')
         else:
               context['registration_form2'] = form
    else:
            form = RegistrationForm()
            context['registration_form2'] = form
    return render(request, 'accounts/register-config.html', context)

The problem is that on registering the second form, the boolean types are not saved into the users profile inside the database and it does not redirects to the home page.
Thanks in advance for your help.

Couple quick items, don’t know if it’s the full set of issues, but there are two things that jump out at me right away:

  1. In registration2, you’re creating an instance of RegistrationForm, not RegistrationForm2

  2. I don’t see where you’re providing the primary key into the second form - somehow you need to associate this second form with the data submitted by the first form - and one way to do this would be to get the primary key from the new object you just created in RegistrationForm, and pass this as a URL parameter in your redirect to the second form. Use that primary key to retrieve the data previously saved and to add the new data in your second form.

Hi KenWhitesell,

Thanks for you help.
Since I have not defined a primary key, would just the username do the job?

Read the section labeled “primary_key” at https://docs.djangoproject.com/en/3.0/topics/db/models/#field-options

You always have a primary_key on models created by django, even if you don’t specifically define one. (Look at your table, you’ll see it’s in there.)

Ken

Thanks, I will have a look at it