More fields in UserCreationForm

This is related to this post. Opposite requirement. I want the form to show email, first_name and last_name too.

class SignUp(generic.CreateView):
    form_class = UserCreationForm
    fields =  ['username', 'password1', 'password2', 'email', 'first_name', 'last_name']
    success_url = reverse_lazy('dashboard')
    template_name = 'registration/signup.html'

    def form_valid(self, form):
        view = super(SignUp, self).form_valid(form)
        username, password, email = form.cleaned_data.get('username'), form.cleaned_data.get('password1'), form.cleaned_data.get('email')
        user = authenticate(username=username, password=password, email=email)
        login(self.request, user)
        return view

But I get

Specifying both ‘fields’ and ‘form_class’ is not permitted.

  1. email should be unique.

  2. What if I want some additional custom fields with custom validation ? Create class UserCreationForm(UserCreationForm): in forms.py ?

The message pretty much explains everything.

I would recommend creating your own form for this. Since the form defines what fields are on the form, that makes the fields attribute unnecessary in the view. (That will also let you implement whatever form validation you need in the form itself.)

But … when I do

    form = UserCreationForm
    fields =  ['username', 'password1', 'password2', 'email', 'first_name', 'last_name']

I get

django.core.exceptions.ImproperlyConfigured: SignUp is missing a QuerySet. Define SignUp.model, SignUp.queryset, or override SignUp.get_queryset().

Yes, you need to specify what model is being created by this view.

So basically on the lines of this ? https://www.py4u.net/discuss/167835

Yes, with answer #1.