[Fixed]-Django UserCreationForm add request.user.pk ?

1👍

You need to pass the user object through the form’s __init__ method.

class SellerRegisterForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user', None)
        super(SellerRegisterForm, self).__init__(*args, **kwargs)

and in your view:

form = SellerRegisterForm(request.POST, user=request.user)

Then simply use self.user anywhere in your form class.

Leave a comment