models层---用户管理
from django.db import models
import hashlib
from django.contrib.auth.models import AbstractUser
models层--基类
- 把公共的字段,定义在基类,其他类继承基类即可
class BaseModel(models.Model):
updated_tm = models.DateTimeField(auto_now=True)
created_tm = models.DateTimeField(auto_now_add=True)
class Meta:
abstract = True
models层--用户基本信息
- 继承基类,即可拥有基类的全部字段
- 继承AbstractUser,实现使用自定义用户列表进行注册登录、身份校验等
- 这里使用密码MD5加密方式,后期使用自定义的用户表做注册登录
- 数据删除使用软删除方式,依然保留已删除的数据
class UserList(AbstractUser, BaseModel):
user_id = models.AutoField(help_text="用户id", primary_key=True)
username = models.SlugField(max_length=128, help_text="用户名", unique=True)
password = models.CharField(max_length=128, help_text="用户密码")
is_disable = models.BooleanField(default=False, help_text='是否禁用')
is_delete = models.BooleanField(default=False, help_text='逻辑删除')
# 指定数据库表信息
class Meta:
db_table = 'user_list'
verbose_name = '用户基础信息'
verbose_name_plural = verbose_name
# 重写save方法,新增密码md5加密逻辑
def save(self, *args, **kwargs):
md5 = hashlib.md5()
md5.update(self.password.encode())
self.password = md5.hexdigest()
super(UserList, self).save(*args, **kwargs)
def delete(self, using=None, keep_parents=False):
"""重写数据库删除方法实现逻辑删除"""
self.is_delete = True
self.save()
models层--用户登录信息
- ForeignKey:一对多关联用户基本信息表
- 插拔式连表查询,用于序列化器,随插随用,后期具体讲
class UserRole(BaseModel):
user_token = models.CharField(max_length=256, help_text="token")
user_info = models.ForeignKey(to=UserList, on_delete=models.DO_NOTHING, db_constraint=False, related_name='user_base', unique=True)
# 指定数据库表信息
class Meta:
db_table = 'user_role'
verbose_name = '用户登录信息'
verbose_name_plural = verbose_name
# 插拔式连表查询
@property
def user_name(self):
return self.user_info.username
@property
def user_id(self):
return self.user_info.user_id
models层--用户详情信息
- ForeignKey:一对多关联用户基本信息表
- 插拔式连表查询,用于序列化器,随插随用,后期具体讲
class UserDetail(BaseModel):
user_email = models.EmailField(blank=True, default="", help_text='用户邮箱')
user_introduction = models.CharField(max_length=128, help_text="用户简介")
nickname = models.CharField(max_length=128, help_text="用户昵称")
user_info = models.ForeignKey(to=UserList, on_delete=models.DO_NOTHING, db_constraint=False, related_name='user')
# 指定数据库表信息
class Meta:
db_table = 'user_detail'
verbose_name = '用户详情信息'
verbose_name_plural = verbose_name
# 插拔式连表查询
@property
def user_name(self):
return self.user_info.username
@property
def user_id(self):
return self.user_info.user_id
models层---项目管理
# 引入之前定义的基类,基类可以放随意地方
from django.db import models
from auto_test_platform.my_user.models import BaseModel
# Create your models here.
models层--项目基本信息
- 继承基类,即可拥有基类的全部字段
- 数据删除使用软删除方式,依然保留已删除的数据
class ProjectList(BaseModel):
project_id = models.AutoField(help_text="项目id", primary_key=True)
project_name = models.CharField(max_length=128, help_text="项目名称")
is_disable = models.BooleanField(default=False, help_text='是否禁用')
is_delete = models.BooleanField(default=False, help_text='逻辑删除')
# 指定数据库表信息
class Meta:
db_table = 'project_list'
verbose_name = '项目基本信息'
verbose_name_plural = verbose_name
def delete(self, using=None, keep_parents=False):
"""重写数据库删除方法实现逻辑删除"""
self.is_delete = True
self.save()
models层--项目详情信息
- ForeignKey:一对多关联项目基本信息表
- 插拔式连表查询,用于序列化器,随插随用,后期具体讲
class ProjectDetail(BaseModel):
project_description = models.CharField(max_length=256, help_text="项目描述")
project_info = models.ForeignKey(to=ProjectList, on_delete=models.DO_NOTHING, db_constraint=False, related_name='project_base', unique=True)
# 指定数据库表信息
class Meta:
db_table = 'project_detail'
verbose_name = '项目详情信息'
verbose_name_plural = verbose_name
# 插拔式连表查询
@property
def project_id(self):
return self.project_info.project_id
@property
def project_name(self):
return self.project_info.project_name
view层---用户管理信息
from auto_test_platform.my_user.models import UserList, UserRole, UserDetail
from rest_framework import mixins, generics
# Create your views here.
class UserListView(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.UpdateModelMixin, generics.GenericAPIView):
# 查询未删除的数据,数据排序方式为新增时间倒序,去掉-,即为顺序排序
queryset = UserList.objects.filter(is_delete=0).all().order_by('-created_tm')
class UserRoleView(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.UpdateModelMixin, generics.GenericAPIView):
# 查询未删除的数据,数据排序方式为新增时间倒序,去掉-,即为顺序排序
queryset = UserRole.objects.filter(is_delete=0).all().order_by('-created_tm')
class UserDetailView(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.UpdateModelMixin, generics.GenericAPIView):
# 查询未删除的数据,数据排序方式为新增时间倒序,去掉-,即为顺序排序
queryset = UserDetail.objects.filter(is_delete=0).all().order_by('-created_tm')
view层-项目列表信息
from rest_framework import mixins, generics
from project.project_list.models import ProjectList
# Create your views here.
class ProjectListView(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.UpdateModelMixin, generics.GenericAPIView):
# 查询未删除的数据,数据排序方式为新增时间倒序,去掉-,即为顺序排序
queryset = ProjectList.objects.filter(is_delete=0).all().order_by('-created_tm')
view层-项目详情信息
from rest_framework import mixins, generics
from project.project_detail.models import ProjectDetail
# Create your views here.
class ProjectDetailView(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.UpdateModelMixin, generics.GenericAPIView):
# 查询未删除的数据,数据排序方式为新增时间倒序,去掉-,即为顺序排序
queryset = ProjectDetail.objects.filter(is_delete=0).all().order_by('-created_tm')