ModelViewSet使用以及配置
model.py
from django.db import models
class Role(models.Model):
# unique=True 代表角色名称不能重复
role_name = models.CharField(max_length=32, unique=True)
class Meta:
db_table = "pp_role1" # 给model类起别名
verbose_name = '角色表'
verbose_name_plural = verbose_name
class Classroom(models.Model):
# unique=True 代表班级名称不能重复
class_name = models.CharField(max_length=32, verbose_name="班级名称", unique=True)
address = models.CharField(max_length=32, null=True, verbose_name='班级地址')
class Meta:
db_table = "pp_class1" # 给model类起别名
verbose_name = '班级表'
verbose_name_plural = verbose_name
def __str__(self):
return self.class_name
class User(models.Model):
username = models.CharField(max_length=32, verbose_name="姓名")
age = models.IntegerField(verbose_name="年龄")
home = models.CharField(verbose_name="家乡", null=True, max_length=32)
hight = models.IntegerField(verbose_name="身高", null=True)
# 多对多
roles = models.ManyToManyField(Role)
# 一对多
classrooms = models.ForeignKey(Classroom, on_delete=models.CASCADE, null=True)
class Meta:
db_table = "pp_user1" # 给model类起别名
verbose_name = '用户表'
verbose_name_plural = verbose_name
Mod