项目后端 - 创建应用 | 创建表
前提:在 luffy 虚拟环境下 1.终端从项目根目录进入apps目录 >: cd luffyapi & cd apps 2.创建app >: python ../../manage.py startapp user
# 创建应用是基于manage.py执行命令,所以要找到manage.py
from django.db import models from django.contrib.auth.models import AbstractUser class User(AbstractUser): mobile = models.CharField(max_length=11, unique=True) 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
INSTALLED_APPS = [ # ... 'user', ] # 自定义User表 因为User表是基于 auth_user 表创建 AUTH_USER_MODEL = 'user.User'
media配置:dev.py
MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
media目录配置
""" ├── luffyapi └── luffyapi/ └── media/ └── icon └── default.png """
数据库迁移
# 数据迁移是基于manage.py执行命令,所以要找到manage.py 迁移命令: python manage.py makemigrations python manage.py migrate
注意: 迁移时会报Pillow模块的错误,因为user表中
安装Pillow模块
>: pip install Pillow
创建admin
>: python manage.py createsuperuser
apps/user/admin/py
from django.contrib import admin from . import models # Register your models here. admin.site.register(models.User)