how to solve this error:
django.core.exceptions.FieldError: Unknown field(s) (contact_number, address, user_type) specified for User.
forms.py:
from django.contrib.auth.models import User
class UserForm(forms.ModelForm):
class Meta:
model=User
fields=['first_name','last_name','address','contact_number','user_type','username','password']
widgets = {'first_name': forms.TextInput(attrs={ 'class': 'form-control'}),
'last_name': forms.TextInput(attrs={ 'class': 'form-control' }),
'address': forms.Textarea(attrs={ 'class': 'form-control' }),
'contact_number': forms.IntegerField(attrs={ 'class': 'form-control' }),
'user_type': forms.Select(attrs={ 'class': 'form-control' }),
'username': forms.TextInput(attrs={ 'class': 'form-control' }),
'password': forms.PasswordInput(attrs={ 'class': 'form-control' })
}
I am getting difficulty here please tell me what to do.
CodePudding user response:
Django's inbuilt User model don't have fields like address
, user_type
, contact_number
Therefore you can create a custom model named as profile
or something else:
from django.db import models
from django.contrib.auth.models import User
Class Profile(models.Model):
parent=models.OneToOneField(User,on_delete=models.CASCADE)
contact=models.CharField(max_length=100)
address=models.CharField(max_length=300)
Now you can co relate your view to this
This is simply a way of extending the User model in django. There are also different ways on django's official website. https://docs.djangoproject.com/en/4.0/topics/auth/customizing/#extending-user