06-user模块user表

user模块user表

一、创建user模块

# 前提:在 luffy 虚拟环境下

# 1.终端从项目根目录进入apps目录
cd luffyapi & cd apps

# 2.创建app
python ../../manage.py startapp user

二、创建User表对应的model:user/models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
    mobile = models.CharField(max_length=11, unique=True)
    # 需要pillow包的支持
    icon = models.ImageField(upload_to='icon', default='icon/default.png')

    class Meta:
        db_table = 'luffy_user'
        verbose_name = '用户表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username

三、注册user模块,配置User表:dev.py

INSTALLED_APPS = [
    # ...
    'user',
]

# 自定义User表
AUTH_USER_MODEL = 'user.user'

四、配置media

1. media配置

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

2. media目录

├── luffyapi
    └──	luffyapi/
       	└──	media/  	
			└──	icon 
				└── default.png

3. 主路由:luffyapi/urls.py

from django.contrib import admin
from django.urls import path, re_path, include
from django.views.static import serve
from django.conf import settings
urlpatterns = [
    path('admin/', admin.site.urls),

    path('user/', include('user.urls')),

    re_path('^media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT})
]

4. 子路由:user/urls.py

from django.urls import path, include
from utils.router import router

# 注册ViewSet的路由
# router.register()

urlpatterns = [
    path('', include(router.urls)),
]

5. 数据库迁移

1)去向大luffyapi所在目录的终端

2)安装pillow模块
pip install pillow

3)数据库迁移(提示: 如果不去大luffyapi所在目录的终端是因为manage.py在那里)
python manage.py makemigrations
python manage.py migrate

五、可能出现的问题及解决方案

1. django.db.migrations.exceptions.NodeNotFoundErros

# 执行python3.6 manage.py makemigrations时抛出异常
django.db.migrations.exceptions.NodeNotFoundError: Migration admin.0002_logentry_user dependencies reference nonexistent parent node ('app01', '0001_initial')
    
django.db.migrations.exceptions。NodeNotFoundError:迁移管理。0002_logentry_user依赖项引用不存在的父节点('app01', '0001_initial')    
    
# 解决:在任意.py文件中导入以下, 删除以下中migrations中的除了__init__.py文件。 (提示:删除了也不要紧, 添加回来即可, 不然会抛出异常)
from django.contrib.admin import migrations
from django.contrib.auth import migrations

image-20210207122202034

2. 未在settings.py声明继承AbstractUser的表出现的异常

# models.py表继承了AbstractUser, 执行数据库迁移时出现异常
SystemCheckError: System check identified some issues:

ERRORS:
app01.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
        HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
app01.User.phone: (fields.E120) CharFields must define a 'max_length' attribute.
app01.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
        HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.
            
            
# 解决:配置文件中没有声明拓展的继承AbstractUser的表
AUTH_USER_MODEL = '应用名.表名'

3. Pillow模块未安装出现的异常

# 错误:
ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
	HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
	HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.
user.User.avatar: (fields.E210) Cannot use ImageField because Pillow is not installed.
	HINT: Get Pillow at https://pypi.org/project/Pillow/ or run command "pip install Pillow".
user.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
	HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
user.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
	HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.

System check identified 5 issues (0 silenced).


# 解决: 安装pillow模块
pip install pillow
posted @ 2021-02-22 16:00  今天捡到一百块钱  阅读(141)  评论(0编辑  收藏  举报