AbstractUser extension of Django User module

I was writing a blog recently, and I just wrote about the user registration and cancellation module. I think this aspect is quite interesting. When trying to open Django's source code, all APIs will not become so intangible. Following the reading of the source code of each module of Django, we can change the code more flexibly to achieve the functions we want.

Now, consider a question. The main requirement is to realize the registration, login and logout function of users in the blog. If you are only satisfied with only registering the user's mailbox or username when registering, Django's own User module can be implemented. But in fact, a common requirement is that registered users should be able to modify their avatar information, email information, nickname information and other more flexible requirements.

You can take a look at the source code of the Django User module first

class User(AbstractUser):
    """
    Users within the Django authentication system are represented by this
    model.

    Username, password and email are required. Other fields are optional.
    """
    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'

Note: If you are Anaconda management, can be a path C:\Users\User\Anaconda3\Lib\site-packages\django\contrib\auth\models.pyView

The User module in Django actually inherits the AbstractUser module, and there are:

  • username
  • first_name
  • last_name
  • email
  • date_joined

As you can see, Userthe module inherits the AbstractUserabstract base class, but merely inherited, and no AbstractUser any extension. Therefore, a need for more demand for User module information, we can inherit AbstractUserand expand according to their needs.

Now, we add some requirements for user attributes, such as supporting users to modify their avatars, supporting user nicknames, qq, wechat, and website links.

class User(AbstractUser):
    nickname = models.CharField(max_length=30, blank=True, null=True, verbose_name='昵称')
    qq = models.CharField(max_length=20, blank=True, null=True, verbose_name='QQ号码')
    url = models.URLField(max_length=100, blank=True, null=True, verbose_name='个人网页地址')
    avatar = ProcessedImageField(upload_to='avatar',
default='avatar/default.png', verbose_name='头像')

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name
        ordering = ['-id']

    def __str__(self):
        return self.username

We add nickname (nickname), qq, url (web link), and avatar (avatar) attributes to the custom user module.

Note: In order for a custom Django can be identified using the user model, must be in the settings.pyset position in a custom module, as settings.pythe added

AUTH_USER_MODEL = 'blog.user'

Wherein the blogapplication corresponding to app your information, user's blog for the next application usermodule, where blogand usercase-insensitive.

If you execute the database migration command now, you may be prompted that the user module does not exist in the blog , and you cannot perform the data migration again.

ValueError: The field account.EmailAddress.user was declared with a lazy reference to 'blog.user', but app 'blog' doesn't provide model 'user'.
The field admin.LogEntry.user was declared with a lazy reference to 'blog.user', but app 'blog' doesn't provide model 'user'.
The field blog.Article.author was declared with a lazy reference to 'blog.user', but app 'blog' doesn't provide model 'user'.
The field easy_comment.Comment.user was declared with a lazy reference to 'blog.user', but app 'blog' doesn't provide model 'user'.
The field easy_comment.Like.user was declared with a lazy reference to 'blog.user', but app 'blog' doesn't provide model 'user'.
The field notifications.Notification.recipient was declared with a lazy reference to 'blog.user', but app 'blog' doesn't provide model 'user'.
The field online_status.OnlineStatus.user was declared with a lazy reference to 'blog.user', but app 'blog' doesn't provide model 'user'.
The field socialaccount.SocialAccount.user was declared with a lazy reference to 'blog.user', but app 'blog' doesn't provide model 'user'.

So, if for example, before using the AUTH_USER_MODEL = auth.useruser model, and relocate it to the custom AUTH_USER_MODEL = blog.userplease delete migrationsall the files in the directory and database files.

After deleting, re-migrate the database

$ python manage.py makemigrations myapp
$ python manage.py migrate

At this time, the user used is the user after customization.

  File "C:\Users\Micky\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Micky\Anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: blog_user

Here you can specify the database in the template db_table = 'user'

Guess you like

Origin blog.csdn.net/qq_36148847/article/details/82599180