Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.core.exceptions.FieldError: Unknown field(s) (academic_grade) specified for User

Tags:

python

django

I am creating a form, inside forms.py I have:

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['first_name', 'last_name',
                  'username', 'email', 'academic_grade',]

But I get the following error:

django.core.exceptions.FieldError: Unknown field(s) (academic_grade) specified for User

I check in the database and user table have the 'academic_grade' grade column

models.py

class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(max_length=30, blank=True, default='')
    academic_grade = models.CharField(choices=GRADE_CHOICES, default='')

please guide me through this

like image 362
matirials Avatar asked Oct 28 '25 09:10

matirials


1 Answers

When you run -

  python manage.py migrate

django will migrate all inbuilt models present in the following apps-

'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes'

From django.contrib.auth app , there is a inbuilt table User , that have the following fields -

 Column    |

 id
 password
 last_login
 is_superuser
 username
 first_name
 last_name
 email
 is_staff
 is_active
 date_joined

I am afraid , this is being picked by the django (either by wrongly import or by running migration command before actually creating your own User). The latter one will actually create User before your models.

Therefore , I would suggest you to either cross check your import or try giving some other name to your user.For e.g

   class PersonForm(forms.ModelForm):
      password = forms.CharField(widget=forms.PasswordInput)

      class Meta:
         model = Person
         fields = ['first_name', 'last_name',
              'username', 'email', 'academic_grade',]

Let me know, if it helps or correct me if you think so !

Thanks

like image 184
tom Avatar answered Oct 31 '25 02:10

tom