# django模型类
# 基类及模型类的创建
1、基类
class BaseMode(models.Model):
# 数据创建时间
created_time = models.DateTimeField(auto_now_add=True, verbose_name="数据创建时间")
# 数据更新时间
update_time = models.DateTimeField(auto_now=True, verbose_name="数据更新时间")
2、运用基类
class Department(BaseMode):
name = models.CharField(max_length=30, verbose_name="部门")
def __str__(self):
return self.name
class Meta:
db_table = "deparment"
class Duty(BaseMode):
name = models.CharField(max_length=50, verbose_name="工作职责")
class Meta:
db_table = "duty"
def __str__(self):
return self.name
3、模型类创建
class Staff(models.Model):
SEX_CHICE = (
(0, '男'),
(1, '女')
)
name = models.CharField(max_length=50, verbose_name="员工姓名")
# 小整型 这个类型是个小型内存
sex = models.SmallIntegerField(default=0, choices=SEX_CHICE, verbose_name="员工性别")
phone = models.CharField(max_length=50, verbose_name="员工手机号")
email = models.CharField(max_length=50, verbose_name="员工的邮箱")
department = models.ForeignKey(to=Department, on_delete=models.CASCADE, verbose_name="外键所属部门")
duty = models.ForeignKey(to=Duty, on_delete=models.CASCADE, verbose_name="外键所属职务")
class Meta:
db_table = "staff"
def __str__(self):
return self.name
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)