# 文章
class Article(models.Model):
title = models.CharField('标题', max_length=70)
excerpt = models.TextField('摘要', max_length=200, blank=True)
category = models.ForeignKey(Category, on_delete=models.DO_NOTHING, verbose_name='分类', blank=True, null=True)
# 使用外键关联分类表与分类是一对多关系
tags = models.ManyToManyField(Tag, verbose_name='标签', blank=True)
# 使用外键关联标签表与标签是多对多关系
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='操作者')
views = models.PositiveIntegerField('阅读量', default=0)
tui = models.ForeignKey(Tui, on_delete=models.DO_NOTHING, verbose_name='推荐位', blank=True, null=True)
created_time = models.DateTimeField('发布时间', auto_now_add=True)
modified_time = models.DateTimeField('修改时间', auto_now=True)

class Meta:
verbose_name = '作业单'
verbose_name_plural = '作业单'

实际生成的mysql表

django model设计与实际数据库表的对比

其中article表

django model设计与实际数据库表的对比

mysql的DDL

CREATE TABLE `les_article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(70) NOT NULL,
`excerpt` longtext NOT NULL,
`views` int(10) unsigned NOT NULL,
`created_time` datetime(6) NOT NULL,
`modified_time` datetime(6) NOT NULL,
`category_id` int(11) DEFAULT NULL,
`tui_id` int(11) DEFAULT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `les_article_category_id_b89e56fb_fk_les_category_id` (`category_id`),
KEY `les_article_tui_id_dfb5096f_fk_les_tui_id` (`tui_id`),
KEY `les_article_user_id_9190afe0_fk_auth_user_id` (`user_id`),
CONSTRAINT `les_article_category_id_b89e56fb_fk_les_category_id` FOREIGN KEY (`category_id`) REFERENCES `les_category` (`id`),
CONSTRAINT `les_article_tui_id_dfb5096f_fk_les_tui_id` FOREIGN KEY (`tui_id`) REFERENCES `les_tui` (`id`),
CONSTRAINT `les_article_user_id_9190afe0_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

 

 其中article_tags表

django model设计与实际数据库表的对比

mysql的DDL

CREATE TABLE `les_article_tags` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`article_id` int(11) NOT NULL,
`tag_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `les_article_tags_article_id_tag_id_ebc98ef0_uniq` (`article_id`,`tag_id`),
KEY `les_article_tags_tag_id_190aec01_fk_les_tag_id` (`tag_id`),
CONSTRAINT `les_article_tags_article_id_d83efe78_fk_les_article_id` FOREIGN KEY (`article_id`) REFERENCES `les_article` (`id`),
CONSTRAINT `les_article_tags_tag_id_190aec01_fk_les_tag_id` FOREIGN KEY (`tag_id`) REFERENCES `les_tag` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

 

 

--------------------------------------------------------

class Category(models.Model):
name = models.CharField('业务分类', max_length=100)
index = models.IntegerField(default=999, verbose_name='分类排序')

class Meta:
verbose_name = '业务分类'
verbose_name_plural = verbose_name

def __str__(self):
return self.name

django model设计与实际数据库表的对比

-------------------------------------------------------------------------------

 

class Tag(models.Model):
name = models.CharField('作业标签', max_length=100)

class Meta:
verbose_name = '作业标签'
verbose_name_plural = verbose_name

def __str__(self):
return self.name

 django model设计与实际数据库表的对比

----------------------------------------------------

 

# 推荐位
class Tui(models.Model):
name = models.CharField('推荐位', max_length=100)

class Meta:
verbose_name = '推荐位'
verbose_name_plural = verbose_name

def __str__(self):
return self.name

 django model设计与实际数据库表的对比

--------------------------------------------------------------

class Link(models.Model):
name = models.CharField('链接名称', max_length=20)
linkurl = models.URLField('网址', max_length=100)

def __str__(self):
return self.name

class Meta:
verbose_name = '友情链接'
verbose_name_plural = '友情链接'

django model设计与实际数据库表的对比