郑文峰的博客 郑文峰的博客
首页
  • python之路
  • go之路
  • 其他
  • redis
  • mysql
  • docker
  • k8s
读书破万卷
周刊
关于
  • 导航 (opens new window)
  • 代码片段 (opens new window)
  • 收藏
  • 友链
  • 外部页面

    • 开往 (opens new window)
  • 索引

    • 分类
    • 标签
    • 归档
GitHub (opens new window)

zhengwenfeng

穷则变,变则通,通则久
首页
  • python之路
  • go之路
  • 其他
  • redis
  • mysql
  • docker
  • k8s
读书破万卷
周刊
关于
  • 导航 (opens new window)
  • 代码片段 (opens new window)
  • 收藏
  • 友链
  • 外部页面

    • 开往 (opens new window)
  • 索引

    • 分类
    • 标签
    • 归档
GitHub (opens new window)
  • python

    • 基础

    • 第三方库

    • django

      • django celery 结合使用
      • django rest_framework使用jwt
      • django rest_framework Authentication
      • django rest_framework异常处理
      • django rest_framework 自定义文档
      • django压缩文件下载
      • django rest_framework使用pytest单元测试
      • django restframework choice 自定义输出数据
      • django Filtering 使用
      • django viewset 和 Router 配合使用时报的错
      • django model的序列化
      • django中使用AbStractUser
      • django.core.exceptions.ImproperlyConfigured Application labels aren't unique, duplicates users
      • django 中 media配置
      • django 外键引用自身和on_delete参数
      • django 警告 while time zone support is active
      • django rest_framework 分页
    • flask

    • tornado

    • 其他

  • go

  • 其他

  • 编程
  • python
  • django
zhengwenfeng
2022-08-10

django中使用AbStractUser

Django内置的User对象,已经包含了一些主要的属性,如username、password、email等,但实际情况可能还需要昵称、头像等其他属性,仅仅使用内置的User属性是不够的。

通过使用AbstractUser可以对User进行扩展使用,添加用户自定义的属性。

User模型源码如下。

class User(AbstractUser):
    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'
1
2
3

由此可见,User对AbstractUser仅仅是继承,没有进行任何的扩展。所以我们继承AbstractUser可以获得User的所有特性。

  • model中使用

继承AbstractUser

from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    pass
1
2
3
4
  • 全局settings.py中设置

覆盖默认的user model

AUTH_USER_MODEL = 'app.MyUser'
1
  • 在admin.py中注册MyUser
from django.contrib import admin
from .models import UserProfile
admin.site.register(UserProfile,UserAdmin)  
#用UserAdmin去注册UserProfile
1
2
3
4
#python#django
上次更新: 2023/01/15, 15:47:48
django model的序列化
django.core.exceptions.ImproperlyConfigured Application labels aren't unique, duplicates users

← django model的序列化 django.core.exceptions.ImproperlyConfigured Application labels aren't unique, duplicates users→

最近更新
01
django rest_framework 分页
03-20
02
学习周刊-第03期-第09周
03-03
03
学习周刊-第02期-第08周
02-24
更多文章>
Theme by Vdoing | Copyright © 2022-2023 zhengwenfeng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式