The article directories

  • Problem description
  • To solve the problem

Problem description

Django comes with a built-in function from Django.contrib. auth import login that can be used for persistent login. The source code of the login function is login(Request, user, Backend =None). In other words, at least two variables are passed, one is request and the other is the data of the current user. Deposited in the database of user data, if it is inherited the Django’s built-in user login system from the Django. Contrib. Auth. The models of the import AbstractUser, passing in a database Model like this to login will not cause the following problems. If your user data is not inherited from AbstractUser, but is custom, just inherited from regular models.model, it will most likely generate an error

  File "Project path \lib\site-packages\ Django \db\models\base.py", line 730.in save
    raise ValueError("The following fields do not exist in this "
ValueError: The following fields do not exist in this model or are m2m fields: last_login
Copy the code

To solve the problem

According to the error prompt, we can see that he can’t find a header for last_login field (this field is used to store the last login time), we only need to add in the user data on the table a last_login = Models. DateTimeField (auto_now_add = True). login(request, user, Backend =None)(where user is the data retrieved from the database that stores user data) If you do not want the last login schedule of the database to be named last_login Db_column = last_login = models.DateTimeField(auto_now_add=True, db_column=’ last login time ‘)