0

I am trying to add some custom fields to a user, and extend the UserCreationForm so that I can add these fields when the user is created. I am following the docs but when I try to load the page to create a user I get an error: Unknown field(s) (username) specified for Customer.

The docs that I am following: Custom User and Auth Forms

models.py

class User(AbstractUser):
    is_restaurant = models.BooleanField(default=False)
    is_customer = models.BooleanField(default=False)

class Customer(models.Model):
    user = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
    address = models.CharField(max_length=200)

    def __str__(self):
        return self.user.get_full_name()

forms.py

class CustomerSignUpForm(UserCreationForm):

    class Meta(UserCreationForm.Meta):
        model = Customer
        fields = UserCreationForm.Meta.fields + ('address',)

I understand that username is not part of the Customer class, but the docs appear to be doing the same thing...

ratrace123
  • 976
  • 4
  • 12
  • 24

1 Answers1

0

The doc says:

If your custom user model is a simple subclass of AbstractUser, then you can extend these forms in this manner...

In other words this will work only in case you want to add to the form is_restaurant or is_customer fields:

 class CustomerSignUpForm(UserCreationForm):

    class Meta(UserCreationForm.Meta):
        model = User
        fields = UserCreationForm.Meta.fields + ('is_restaurant',)

But in your case Customer is not subclass of AbstractUser, since this method is not working for you. As a workaround you can try to work with two separate forms in the same time as suggested in this answer.

neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
  • 1
    hello, thanks so much I was looking right past the fact that it wasnt subclassing `AbstractUser`. I will try the method in the link to the answer you provided, thanks again! – ratrace123 Feb 21 '18 at 05:23