首先,这是个新模块,在apps下创建projects
1.1、django-admin startapp projects
1.2进行注册
‘apps.projects.apps.ProjectsConfig’
进行接口编写的过程中,落入数据库的字段相同的有很多我们要在utils文件下进行闯进模型鸡肋
from django.db import models
class BaseModel(models.Model):
"""
基类,公共字段
"""
c_time = models.DateTimeField('创建时间', auto_now_add=True)
u_time = models.DateTimeField('更新时间', auto_now=True)
is_delete = models.BooleanField('逻辑删除', default=False)
class Meta:
# 抽象类,用于继承,迁移时不会创建
abstract = True
进行项目模块模型创建
在projects包下views.py文件下编写代码
项目模型
环境模型
def validate_host(value):if value.endswith('/'):raise ValidationError('host不能以/结尾')class Environment(BaseModel):# 环境模型db_config_help = '''json数据 {"host": "some.com","user": "root","password": "123456","db": "some","charset": "utf8","autocommit": true}'''name = models.CharField('环境名称', max_length=128, help_text='环境名称')project = models.ForeignKey(Project, on_delete=models.CASCADE, help_text='所属项目')host = models.URLField('主机地址', validators=[validate_host], help_text='url后不要以/结尾')db_config = models.JSONField('数据库设置', null=True, blank=True, help_text=db_config_help)def __str__(self):return '{}_{}'.format(self.project.name, self.name)class Meta:db_table = 'tb_environment'verbose_name = '项目环境'verbose_name_plural = verbose_name# 唯一约束constraints = [models.UniqueConstraint(fields=['name', 'project'], name='unique_env_name')]
模块模型
class Module(BaseModel):name = models.CharField('模块名称', max_length=40, help_text='模块名称')project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='所属项目')def __str__(self):return '{}_{}'.format(self.project.name, self.name)class Meta:db_table = 'tb_module'verbose_name = '功能模块'verbose_name_plural = verbose_nameconstraints = [# 模块名和项目id联合唯一models.UniqueConstraint(fields=['name', 'project'], name='unique_module_name')]
接口模型
class Interface(BaseModel):name = models.CharField('接口名称', max_length=40, help_text='接口名称')module = models.ForeignKey(Module, on_delete=models.PROTECT, help_text='所属模块', related_name='interfaces')developer = models.CharField('开发者', max_length=40, help_text='开发者', default='')url = models.CharField('接口地址', max_length=200, help_text='接口地址')def __str__(self):return '{}_{}_{}'.format(self.module.project.name, self.module.name, self.name)class Meta:db_table = 'tb_interface'verbose_name = '接口详情'verbose_name_plural = verbose_name# 接口名和模块id联合唯一constraints = [models.UniqueConstraint(fields=['name', 'module'], name='unique_interface_name')]
模型编写完毕
接下来进行数据迁移
两个命令行
数据执行,查看数据库,相关的表。
到这projects模型数据库的相关工作结束,下面时接口相关的编写。