############### 新建APP并配置 ###############
INSTALLED_APPS = [ ... 'apps.users.apps.UsersConfig', 'apps.rbac.apps.RbacConfig', 'apps.system.apps.SystemConfig', 'apps.adm.apps.AdmConfig', 'apps.personal.apps.PersonalConfig', ]
############### 表结构-users ###############
from datetime import datetime from django.db import models from django.contrib.auth.models import AbstractUser from rbac.models import Role class UserProfile(AbstractUser): """ 用户: makemigration提示错误:sers.UserProfile.user_permissions: (fields.E304), 需要在settings中指定自定义认证模型:AUTH_USER_MODEL = 'users.UserProfile' """ name = models.CharField(max_length=20, default="", verbose_name="姓名") birthday = models.DateField(null=True, blank=True, verbose_name="出生日期") gender = models.CharField(max_length=10, choices=(("male", "男"), ("famale", "女")), default="male", verbose_name="性别") mobile = models.CharField(max_length=11, default="", verbose_name="电话") email = models.EmailField(max_length=100, verbose_name="邮箱") image = models.ImageField(upload_to="image/%Y/%m", default="image/default.jpg", max_length=100, null=True, blank=True) department = models.ForeignKey("Structure", null=True, blank=True, verbose_name="部门") post = models.CharField(max_length=50, null=True, blank=True, verbose_name="职位") superior = models.ForeignKey("self", null=True, blank=True, verbose_name="上级主管") roles = models.ManyToManyField("rbac.Role", verbose_name="角色", blank=True) joined_date = models.DateField(null=True, blank=True, verbose_name="入职日期") class Meta: verbose_name = "用户信息" verbose_name_plural = verbose_name ordering = ['id'] def __str__(self): return self.name class Structure(models.Model): """ 组织架构 """ type_choices = (("firm", "公司"), ("department", "部门")) title = models.CharField(max_length=60, verbose_name="名称") type = models.CharField(max_length=20, choices=type_choices, default="department", verbose_name="类型") parent = models.ForeignKey("self", null=True, blank=True, verbose_name="父类架构") class Meta: verbose_name = "组织架构" verbose_name_plural = verbose_name def __str__(self): return self.title
############### 表结构-system ###############
from django.db import models # Create your models here. class SystemSetup(models.Model): loginTitle = models.CharField(max_length=20, null=True, blank=True, verbose_name='登录标题') mainTitle = models.CharField(max_length=20, null=True, blank=True, verbose_name='系统标题') headTitle = models.CharField(max_length=20, null=True, blank=True, verbose_name='浏览器标题') copyright = models.CharField(max_length=100, null=True, blank=True, verbose_name='底部版权信息') url = models.CharField(max_length=50, null=True, blank=True, verbose_name='系统URL地址') def __str__(self): return self.loginTitle class Meta: verbose_name = "系统设置" verbose_name_plural = verbose_name @classmethod def getSystemSetupLastData(self): return dict(system_setup=SystemSetup.objects.last()) class EmailSetup(models.Model): emailHost = models.CharField(max_length=30, verbose_name='SMTP服务器') emailPort = models.IntegerField(verbose_name='SMTP端口') emailUser = models.EmailField(max_length=100, verbose_name='邮箱帐号') emailPassword = models.CharField(max_length=30, verbose_name='邮箱密码') def __str__(self): return self.emailHost class Meta: verbose_name = '发件邮箱设置' verbose_name_plural = verbose_name @classmethod def getEmailSetupLastData(self): return EmailSetup.objects.last()
############### 表结构-rbac ###############
from django.db import models class Menu(models.Model): """ 菜单 """ title = models.CharField(max_length=32, unique=True, verbose_name="菜单名") parent = models.ForeignKey("self", null=True, blank=True, verbose_name="父菜单") is_top = models.BooleanField(default=False, verbose_name="首页显示") icon = models.CharField(max_length=50, null=True, blank=True, verbose_name="图标") code = models.CharField(max_length=50, null=True, blank=True, verbose_name="编码") url = models.CharField(max_length=128, unique=True, null=True, blank=True) def __str__(self): title_list = [self.title] p = self.parent while p: title_list.insert(0, p.title) p = p.parent return '-'.join(title_list) class Meta: verbose_name = "菜单" verbose_name_plural = verbose_name @classmethod def getMenuByRequestUrl(self, url): ret = dict(menu=Menu.objects.get(url=url)) return ret class Role(models.Model): """ 角色:绑定权限 """ title = models.CharField(max_length=32) permissions = models.ManyToManyField("menu", blank=True) def __str__(self): return self.title class Meta: verbose_name = "角色" verbose_name_plural = verbose_name
############### 表结构-personal ###############
from django.db import models from django.contrib.auth import get_user_model from adm.models import Customer User = get_user_model() class WorkOrder(models.Model): type_choices = (('0', '初次安装'), ('1', '售后现场'), ('2', '远程支持'), ('3', '售前支持')) status_choices = (('0', '工单已退回'), ('1', '新建-保存'), ('2', '提交-等待审批'), ('3', '已审批-等待执行'), ('4', '已执行-等待确认'), ('5', '工单已完成')) number = models.CharField(max_length=10, verbose_name='工单号') title = models.CharField(max_length=50, verbose_name='标题') type = models.CharField(max_length=10, choices=type_choices, default='0', verbose_name='工单类型') status = models.CharField(max_length=10, choices=status_choices, default='0', verbose_name='工单状态') do_time = models.DateTimeField(default='', verbose_name='安排时间') add_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间') content = models.CharField(max_length=300, verbose_name='工单内容') file_content = models.FileField(upload_to='file/%Y/%m', blank=True, null=True, verbose_name='项目资料') customer = models.ForeignKey(Customer, verbose_name='客户信息') proposer = models.ForeignKey(User, related_name='proposer', blank=True, null=True, on_delete=models.SET_NULL, verbose_name='申请人') approver = models.ForeignKey(User, related_name='approver', blank=True, null=True, on_delete=models.SET_NULL, verbose_name='审批人') receiver = models.ForeignKey(User, related_name='receiver', blank=True, null=True, on_delete=models.SET_NULL, verbose_name='接单人') class Meta: verbose_name = '工单信息' verbose_name_plural = verbose_name def __str__(self): return self.title class WorkOrderRecord(models.Model): type_choices = (('0', '退回'), ('1', "派发"), ('2', "执行"), ('3', "确认")) name = models.ForeignKey(User, verbose_name=u"记录人") work_order = models.ForeignKey(WorkOrder, verbose_name=u"工单信息") record_type = models.CharField(max_length=10, choices=type_choices, verbose_name=u"记录类型") content = models.CharField(max_length=500, verbose_name=u"记录内容", default="") file_content = models.FileField(upload_to='file/%Y/%m', blank=True, null=True, verbose_name='实施文档') add_time = models.DateTimeField(auto_now_add=True, verbose_name=u"记录时间") class Meta: verbose_name = u"执行记录" verbose_name_plural = verbose_name def __str__(self): return self.record_type
############### 表结构-adm ###############
from django.db import models from django.contrib.auth import get_user_model User = get_user_model() class Supplier(models.Model): """ 分销商管理 """ company = models.CharField(max_length=30, verbose_name="公司名称") address = models.CharField(max_length=100, verbose_name="地址") linkname = models.CharField(max_length=20, verbose_name="联系人") phone = models.CharField(max_length=20, verbose_name="联系电话") status = models.BooleanField(default=True, verbose_name="状态") belongs_to = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL, verbose_name="责任人") desc = models.TextField(blank=True, null=True, verbose_name="备注") add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间") class Meta: verbose_name = "分销商管理" verbose_name_plural = verbose_name def __str__(self): return self.company class Customer(models.Model): """ 客户信息 """ unit = models.CharField(max_length=50, verbose_name="客户单位") address = models.CharField(max_length=100, verbose_name="地址") name = models.CharField(max_length=20, verbose_name="联系人") phone = models.CharField(max_length=30, verbose_name="联系电话") belongs_to = models.ForeignKey(User, blank=True, null=True , on_delete=models.SET_NULL, verbose_name="责任人") status = models.BooleanField(default=True, verbose_name="状态") desc = models.TextField(blank=True, null=True, verbose_name="备注") add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间") class Meta: verbose_name = "客户管理" verbose_name_plural = verbose_name def __str__(self): return self.unit class AssetType(models.Model): """ 资产类型 """ name = models.CharField(max_length=30, verbose_name="类型名称", help_text="类型名称") desc = models.TextField(blank=True, null=True, verbose_name="备注") class Meta: verbose_name = "资产类型" def __str__(self): return self.name class Asset(models.Model): asset_status = ( ("0", "闲置"), ("1", "在用"), ("2", "维修"), ("3", "报废"), ("4", "售出") ) warehouse_choices = ( ("0", "南京"), ("1", "苏州"), ) assetNum = models.CharField(max_length=128, default="", verbose_name="资产编号") assetType = models.ForeignKey(AssetType, blank=True, null=True, on_delete=models.SET_NULL, verbose_name="资产类型") brand = models.CharField(max_length=20, blank=True, null=True, verbose_name="品牌") model = models.CharField(max_length=30, default="", verbose_name="型号") warehouse = models.CharField(choices=warehouse_choices, default="1", max_length=20, verbose_name="仓库") price = models.IntegerField(blank=True, null=True, verbose_name="价格") buyDate = models.DateField(verbose_name="购买日期") warrantyDate = models.DateField(verbose_name="到保日期") status = models.CharField(choices=asset_status, max_length=20, default="1", verbose_name="资产状态") customer = models.CharField(max_length=80, default="", blank=True, null=True, verbose_name="客户信息") owner = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL, verbose_name="使用人") operator = models.CharField(max_length=20, default="", verbose_name="入库人") add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间") desc = models.TextField(default="", blank=True, null=True, verbose_name="备注信息") class Meta: verbose_name = "资产管理" verbose_name_plural = verbose_name def __str__(self): return self.assetNum class AssetFile(models.Model): asset = models.ForeignKey(Asset, blank=True, null=True, on_delete=models.SET_NULL, verbose_name="资产") upload_user = models.CharField(max_length=20, verbose_name="上传人") file_content = models.ImageField(upload_to="asset_file/%Y/%m", null=True, blank=True, verbose_name="资产文件") add_time = models.DateTimeField(auto_now_add=True, verbose_name="上传时间") class AssetLog(models.Model): asset = models.ForeignKey(Asset, verbose_name="资产") operator = models.CharField(max_length=20, verbose_name="操作人") desc = models.TextField(default="", verbose_name="备注") add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间") class Mate: verbose_name = "变更记录" verbose_name_plural = verbose_name def __str__(self): return self.asset class ServiceInfo(models.Model): content = models.TextField(verbose_name="记录内容") writer = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL, verbose_name="记录人") is_reminding = models.BooleanField(default=False, verbose_name="邮件消息提醒") add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间") class Mate: verbose_name = "服务记录" verbose_name_plural = verbose_name def __str__(self): return self.content class EquipmentType(models.Model): """ 设备类型 """ name = models.CharField(max_length=30, verbose_name="类型名称", help_text="类型名称") desc = models.TextField(blank=True, null=True, verbose_name="备注") class Meta: verbose_name = "设备类型" verbose_name_plural = verbose_name ordering = ['id'] def __str__(self): return self.name class Equipment(models.Model): number = models.CharField(max_length=30, default="", verbose_name="设备编号") equipment_type = models.ForeignKey(EquipmentType, blank=True, null=True, on_delete=models.SET_NULL, verbose_name="设备类型") equipment_model = models.CharField(max_length=50, default="", verbose_name="设备型号") buy_date = models.DateField(verbose_name="购买日期") warranty_date = models.DateField(verbose_name="质保日期") accounting = models.BooleanField(default=False, verbose_name="费用核算状态") config_desc = models.TextField(blank=True, null=True, verbose_name="配置说明") customer = models.ForeignKey(Customer, blank=True, null=True, on_delete=models.SET_NULL, verbose_name="客户信息") supplier = models.ForeignKey(Supplier, blank=True, null=True, on_delete=models.SET_NULL, verbose_name="分销商") service_info = models.ManyToManyField(ServiceInfo, blank=True, verbose_name="服务记录") class Meta: verbose_name = "设备管理" verbose_name_plural = verbose_name def __str__(self): return self.number
############### 数据库迁移 ###############
数据库迁移:
生成迁移 python manage.py makemigrations
执行迁移 python manage.py migrate
############### auth配置 ###############
AUTH_USER_MODEL = 'users.UserProfile'
############### 表结构 ###############