3
votes

apologies in advance if this is a duplicate question. To the best of my searches, I was not able to find this question.

Using Django 2.0, I substituted the built-in django.contrib.auth with my own Users model, extending it with AbstractUser. The Users model is dependent on an existing table in MySQL, which is configured to work with Django. Django does not detect any issues when performing a check; however, I receive the following error:

OperationalError at / (1054, "Unknown column 'users.last_login' in 'field list'")

I added the column into the table thinking it would be a useful field to have, but was apprehensive because I figured I'd get another error for another missing field. Lo and behold:

OperationalError at / (1054, "Unknown column 'users.is_superuser' in 'field list'")

The point of substituting the user model is so I can ditch unimportant fields, such as "username". Do I just need to add each column to the table until I get all the auth fields in the table, or am I doing something fundamentally wrong?

TL;DR: How do I create a custom authentication method for Django using an existing table?

1

1 Answers

1
votes

As you are saying, you are getting these errors, because your table doesn't have the columns for fields defined in AbstractUser model. The easiest solution is to just add all the missing fields to your table.

Alternatively, if you want to avoid that, you can have your user model subclass AbstractBaseUser instead. This abstract model has only fields for password and last_login. Note, that this approach is going to be a bit more work. This seems to be a good guide on how to do it:

https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html