Search code examples
djangodjango-formsdjango-templatesdjango-registration

View with multiple forms not saving (missing 1 positional argument on form)


I have setup an override for Django-Registration-Redux to save on an additional model UserProfile below:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    tel_number = models.CharField(max_length=20, null = True)

This is setup with the following form:

class UserProfileRegistrationForm(RegistrationFormUniqueEmail):
    tel_number = forms.CharField()

And regbackend.py:

from registration.backends.default.views import RegistrationView
from .forms import UserProfileRegistrationForm
from .models import UserProfile

class MyRegistrationView(RegistrationView):

    form_class = UserProfileRegistrationForm

    def register(self, request, form_class):
        new_user = super(MyRegistrationView, self).register(request, form_class)
        user_profile = UserProfile()
        user_profile.user = new_user
        user_profile.tel_number = form_class.cleaned_data['tel_number']
        user_profile.save()
        return user_profile

However, even though the view loads correctly and I can fill it out, I get the following error each time I save:

register() missing 1 required positional argument: 'form_class'

I assume this is something passing incorrectly?


Solution

  • I checked the code quickly here:

    https://github.com/macropin/django-registration/blob/master/registration/backends/default/views.py

    That Django-Registration-Redux seems to implement in RegistrationView, the register method like so:

    def register(self, form):
       #code
    

    Try to remove request and it should work, sth like this.

    def register(self, form_class):
       new_user = super(MyRegistrationView, self).register(form_class)
       #code