ShippyDippy
ShippyDippy

Reputation: 53

How do I add custom field in to model that extends AbstractUser

I'm trying to create my own User class called Customer which extends the AbstractUser model and has an additional field called address. When I register, I see the user has been created in Django admin and all the fields (username, first name, last name and email) are seen in the django admin screen but I see no value in the "address" field. How do I know if the address is being saved and how can I display it in the admin site?

Below is my code for the models.py

class Customer(AbstractUser):
    username = models.CharField(unique=True, max_length=20)
    deladdress = models.CharField(max_length=100)

views.py

def signupPage(request):
    signForm = CreateCustomer()
    if request.method=='POST':
        signForm = CreateCustomer(request.POST)
        if signForm.is_valid():
            signForm.save()

    return render(request, 'trial_app/signup.html', {'signForm':signForm})

forms.py

class CreateCustomer(UserCreationForm):
    address = forms.CharField(widget=forms.Textarea)
    class Meta:
        model = Customer
        fields = ['username','first_name','last_name','email','address','password1','password2']

    def save(self, commit=True):
        user = super(CreateCustomer, self).save(commit=False)
        user.address = self.cleaned_data["address"]
        if commit:
            user.save()
        return user

Here are some pictures that are the input to my html form and the value in the admin site enter image description here

enter image description here

Upvotes: 0

Views: 466

Answers (1)

Dmytro
Dmytro

Reputation: 340

It seems like when you save a form in its save method you use user.address = self.cleaned_data["address"], however, Customer model does not have address field, it has deladdress, so try to rename a field, or use user.deladdress in your save method of CreateCustomer form.

Upvotes: 1

Related Questions