from django.db import models
models.CharField()
# Create your models here.
class Projects(models.Model):
name = models.CharField(verbose_name=‘项目名称‘, max_length=64, unique=True, help_text=‘项目名称‘)
leader = models.CharField(verbose_name=‘负责人‘, max_length=64, help_text=‘负责人‘)
tester = models.CharField(verbose_name=‘测试人员‘, max_length=64, help_text=‘测试人员‘)
developer = models.CharField(verbose_name=‘开发人员‘, max_length=64, help_text=‘开发人员‘)
publish_app = models.CharField(verbose_name=‘发布应用‘, max_length=200, help_text=‘发布应用‘)
# 长文本:TextField 前端可不传递:blank 默认值:default 允许为空:null
desc = models.TextField(verbose_name=‘简要描述‘, blank=True, default=‘‘, null=True, help_text=‘简要描述‘)
# 定义子类:设置当前数据模型的元信息
class Meta:
db_table = ‘tb_project‘
# 在admin站点中,显示一个更加人性化的表明
verbose_name = ‘项目‘
verbose_name_plural = ‘项目‘
class Book(models.Model):
title = models.CharField(max_length=64, verbose_name="书名")
price = models.IntegerField(verbose_name="价格", editable=True)
color = models.CharField(max_length=64, verbose_name="颜色")
page_num = models.IntegerField(null=True, editable=True)
# 一对多
publisher = models.ForeignKey("Publish", on_delete=models.CASCADE, verbose_name="出版社")
# 创建个新表
author = models.ManyToManyField("Author", verbose_name="作者")
# ManyToManyField: 可用两个ForeignKey
def __str__(self):
return self.title
class Author(models.Model):
name = models.CharField(max_length=30)
def __str__(self):
return self.name
# class Book_To_Author(models.Model):
# book = models.ForeignKey("Book", on_delete=models.CASCADE)
# author = models.ForeignKey("Author", on_delete=models.CASCADE)
# 组合唯一
# class Mete:
# unique_together = ["book", "author"]
class Publish(models.Model):
name = models.CharField(max_length=64)
city = models.CharField(max_length=63)
def __str__(self):
return self.name
原文:https://www.cnblogs.com/zxy01/p/14013314.html