表结构设计1详细注释代码
# _*_coding:utf-8_*_
__author__ = 'luoahong' from assets.myauth import UserProfile
from django.db import models class Asset(models.Model):
asset_type_choices = (
('server', u'服务器'),
('networkdevice', u'网络设备'),
('storagedevice', u'存储设备'),
('securitydevice', u'安全设备'),
('securitydevice', u'机房设备'),
# ('switch', u'交换机'),
# ('router', u'路由器'),
# ('firewall', u'防火墙'),
# ('storage', u'存储设备'),
# ('NLB', u'NetScaler'),
# ('wireless', u'无线AP'),
('software', u'软件资产'),
# ('others', u'其它类'),
)
asset_type = models.CharField(choices=asset_type_choices, max_length=64, default='server')
name = models.CharField(max_length=64, unique=True)
sn = models.CharField(u'资产SN号', max_length=128, unique=True)
manufactory = models.ForeignKey('Manufactory', verbose_name=u'制造商', null=True, blank=True)
# model = models.ForeignKey('ProductModel', verbose_name=u'型号')
# model = models.CharField(u'型号',max_length=128,null=True, blank=True ) management_ip = models.GenericIPAddressField(u'管理IP', blank=True, null=True) contract = models.ForeignKey('Contract', verbose_name=u'合同', null=True, blank=True)
trade_date = models.DateField(u'购买时间', null=True, blank=True)
expire_date = models.DateField(u'过保修期', null=True, blank=True)
price = models.FloatField(u'价格', null=True, blank=True)
business_unit = models.ForeignKey('BusinessUnit', verbose_name=u'所属业务线', null=True, blank=True)
tags = models.ManyToManyField('Tag', blank=True)
admin = models.ForeignKey('UserProfile', verbose_name=u'资产管理员', null=True, blank=True)
idc = models.ForeignKey('IDC', verbose_name=u'IDC机房', null=True, blank=True) status_choices = ((0, '在线'),
(1, '已下线'),
(2, '未知'),
(3, '故障'),
(4, '备用'),
)
status = models.SmallIntegerField(choices=status_choices, default=0)
# status = models.ForeignKey('Status', verbose_name = u'设备状态',default=1)
# Configuration = models.OneToOneField('Configuration',verbose_name='配置管理',blank=True,null=True) memo = models.TextField(u'备注', null=True, blank=True)
create_date = models.DateTimeField(blank=True, auto_now_add=True)
update_date = models.DateTimeField(blank=True, auto_now=True) class Meta:
verbose_name = '资产总表'
verbose_name_plural = "资产总表" def __str__(self):
return '<id:%s name:%s>' % (self.id, self.name) class Server(models.Model):
"""服务器设备"""
asset = models.OneToOneField('Asset')
sub_assset_type_choices = (
(0, 'PC服务器'),
(1, '刀片机'),
(2, '小型机'),
)
created_by_choices = (
('auto', 'Auto'),
('manual', 'Manual'),
)
sub_asset_type = models.SmallIntegerField(choices=sub_assset_type_choices, verbose_name="服务器类型", default=0)
created_by = models.CharField(choices=created_by_choices, max_length=32,
default='auto') # auto: auto created, manual:created manually
hosted_on = models.ForeignKey('self', related_name='hosted_on_server', blank=True, null=True) # for vitural server
# sn = models.CharField(u'SN号',max_length=128)
# management_ip = models.CharField(u'管理IP',max_length=64,blank=True,null=True)
# manufactory = models.ForeignKey(verbose_name=u'制造商',max_length=128,null=True, blank=True)
model = models.CharField(verbose_name=u'型号', max_length=128, null=True, blank=True)
# 若有多个CPU,型号应该都是一致的,故没做ForeignKey # nic = models.ManyToManyField('NIC', verbose_name=u'网卡列表')
# disk
raid_type = models.CharField(u'raid类型', max_length=512, blank=True, null=True)
# physical_disk_driver = models.ManyToManyField('Disk', verbose_name=u'硬盘',blank=True,null=True)
# raid_adaptor = models.ManyToManyField('RaidAdaptor', verbose_name=u'Raid卡',blank=True,null=True)
# memory
# ram_capacity = models.IntegerField(u'内存总大小GB',blank=True)
# ram = models.ManyToManyField('Memory', verbose_name=u'内存配置',blank=True,null=True) os_type = models.CharField(u'操作系统类型', max_length=64, blank=True, null=True)
os_distribution = models.CharField(u'发型版本', max_length=64, blank=True, null=True)
os_release = models.CharField(u'操作系统版本', max_length=64, blank=True, null=True) class Meta:
verbose_name = '服务器'
verbose_name_plural = "服务器"
# together = ["sn", "asset"] def __str__(self):
return '%s sn:%s' % (self.asset.name, self.asset.sn) class SecurityDevice(models.Model):
"""安全设备"""
asset = models.OneToOneField('Asset')
sub_assset_type_choices = (
(0, '防火墙'),
(1, '入侵检测设备'),
(2, '互联网网关'),
(4, '运维审计系统'),
)
sub_asset_type = models.SmallIntegerField(choices=sub_assset_type_choices, verbose_name="服务器类型", default=0) def __str__(self):
return self.asset.id class NetworkDevice(models.Model):
"""网络设备""" asset = models.OneToOneField('Asset')
sub_assset_type_choices = (
(0, '路由器'),
(1, '交换机'),
(2, '负载均衡'),
(4, 'VPN设备'),
)
sub_asset_type = models.SmallIntegerField(choices=sub_assset_type_choices, verbose_name="服务器类型", default=0) vlan_ip = models.GenericIPAddressField(u'VlanIP', blank=True, null=True)
intranet_ip = models.GenericIPAddressField(u'内网IP', blank=True, null=True)
# sn = models.CharField(u'SN号',max_length=128,unique=True)
# manufactory = models.CharField(verbose_name=u'制造商',max_length=128,null=True, blank=True)
model = models.CharField(u'型号', max_length=128, null=True, blank=True)
firmware = models.ForeignKey('Software', blank=True, null=True)
port_num = models.SmallIntegerField(u'端口个数', null=True, blank=True)
device_detail = models.TextField(u'设置详细配置', null=True, blank=True) class Meta:
verbose_name = '网络设备'
verbose_name_plural = "网络设备" class Software(models.Model):
'''
only save software which company purchased
'''
sub_assset_type_choices = (
(0, 'OS'),
(1, '办公\开发软件'),
(2, '业务软件'), )
sub_asset_type = models.SmallIntegerField(choices=sub_assset_type_choices, verbose_name="服务器类型", default=0)
license_num = models.IntegerField(verbose_name="授权数")
# os_distribution_choices = (('windows','Windows'),
# ('centos','CentOS'),
# ('ubuntu', 'Ubuntu'))
# type = models.CharField(u'系统类型', choices=os_types_choice, max_length=64,help_text=u'eg. GNU/Linux',default=1)
# distribution = models.CharField(u'发型版本', choices=os_distribution_choices,max_length=32,default='windows')
version = models.CharField(u'软件/系统版本', max_length=64, help_text=u'eg. CentOS release 6.5 (Final)', unique=True) # language_choices = (('cn',u'中文'),
# ('en',u'英文'))
# language = models.CharField(u'系统语言',choices = language_choices, default='cn',max_length=32)
# #version = models.CharField(u'版本号', max_length=64,help_text=u'2.6.32-431.3.1.el6.x86_64' ) def __str__(self):
return self.version class Meta:
verbose_name = '软件/系统'
verbose_name_plural = "软件/系统" class CPU(models.Model):
"""CPU组件""" asset = models.OneToOneField('Asset')
cpu_model = models.CharField(u'CPU型号', max_length=128, blank=True)
cpu_count = models.SmallIntegerField(u'物理cpu个数')
cpu_core_count = models.SmallIntegerField(u'cpu核数')
memo = models.TextField(u'备注', null=True, blank=True)
create_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(blank=True, null=True) class Meta:
verbose_name = 'CPU部件'
verbose_name_plural = "CPU部件" def __str__(self):
return self.cpu_model class RAM(models.Model):
"""内存组件""" asset = models.ForeignKey('Asset')
sn = models.CharField(u'SN号', max_length=128, blank=True, null=True)
model = models.CharField(u'内存型号', max_length=128)
slot = models.CharField(u'插槽', max_length=64)
capacity = models.IntegerField(u'内存大小(MB)')
memo = models.CharField(u'备注', max_length=128, blank=True, null=True)
create_date = models.DateTimeField(blank=True, auto_now_add=True)
update_date = models.DateTimeField(blank=True, null=True) def __str__(self):
return '%s:%s:%s' % (self.asset_id, self.slot, self.capacity) class Meta:
verbose_name = 'RAM'
verbose_name_plural = "RAM"
unique_together = ("asset", "slot") auto_create_fields = ['sn', 'slot', 'model', 'capacity'] class Disk(models.Model):
"""硬盘组件""" asset = models.ForeignKey('Asset')
sn = models.CharField(u'SN号', max_length=128, blank=True, null=True)
slot = models.CharField(u'插槽位', max_length=64)
# manufactory = models.CharField(u'制造商', max_length=64,blank=True,null=True)
model = models.CharField(u'磁盘型号', max_length=128, blank=True, null=True)
capacity = models.FloatField(u'磁盘容量GB')
disk_iface_choice = (
('SATA', 'SATA'),
('SAS', 'SAS'),
('SCSI', 'SCSI'),
('SSD', 'SSD'),
) iface_type = models.CharField(u'接口类型', max_length=64, choices=disk_iface_choice, default='SAS')
memo = models.TextField(u'备注', blank=True, null=True)
create_date = models.DateTimeField(blank=True, auto_now_add=True)
update_date = models.DateTimeField(blank=True, null=True) auto_create_fields = ['sn', 'slot', 'manufactory', 'model', 'capacity', 'iface_type'] class Meta:
unique_together = ("asset", "slot")
verbose_name = '硬盘'
verbose_name_plural = "硬盘" def __str__(self):
return '%s:slot:%s capacity:%s' % (self.asset_id, self.slot, self.capacity) class NIC(models.Model):
"""网卡组件""" asset = models.ForeignKey('Asset')
name = models.CharField(u'网卡名', max_length=64, blank=True, null=True)
sn = models.CharField(u'SN号', max_length=128, blank=True, null=True)
model = models.CharField(u'网卡型号', max_length=128, blank=True, null=True)
macaddress = models.CharField(u'MAC', max_length=64, unique=True)
ipaddress = models.GenericIPAddressField(u'IP', blank=True, null=True)
netmask = models.CharField(max_length=64, blank=True, null=True)
bonding = models.CharField(max_length=64, blank=True, null=True)
memo = models.CharField(u'备注', max_length=128, blank=True, null=True)
create_date = models.DateTimeField(blank=True, auto_now_add=True)
update_date = models.DateTimeField(blank=True, null=True) def __str__(self):
return '%s:%s' % (self.asset_id, self.macaddress) class Meta:
verbose_name = u'网卡'
verbose_name_plural = u"网卡"
# unique_together = ("asset_id", "slot")
unique_together = ("asset", "macaddress") auto_create_fields = ['name', 'sn', 'model', 'macaddress', 'ipaddress', 'netmask', 'bonding'] class RaidAdaptor(models.Model):
"""Raid卡""" asset = models.ForeignKey('Asset')
sn = models.CharField(u'SN号', max_length=128, blank=True, null=True)
slot = models.CharField(u'插口', max_length=64)
model = models.CharField(u'型号', max_length=64, blank=True, null=True)
memo = models.TextField(u'备注', blank=True, null=True)
create_date = models.DateTimeField(blank=True, auto_now_add=True)
update_date = models.DateTimeField(blank=True, null=True) def __str__(self):
return self.name class Meta:
unique_together = ("asset", "slot") class Manufactory(models.Model):
"""厂商""" manufactory = models.CharField(u'厂商名称', max_length=64, unique=True)
support_num = models.CharField(u'支持电话', max_length=30, blank=True)
memo = models.CharField(u'备注', max_length=128, blank=True) def __str__(self):
return self.manufactory class Meta:
verbose_name = '厂商'
verbose_name_plural = "厂商" class BusinessUnit(models.Model):
"""业务线""" parent_unit = models.ForeignKey('self', related_name='parent_level', blank=True, null=True)
name = models.CharField(u'业务线', max_length=64, unique=True) # contact = models.ForeignKey('UserProfile',default=None)
memo = models.CharField(u'备注', max_length=64, blank=True) def __str__(self):
return self.name class Meta:
verbose_name = '业务线'
verbose_name_plural = "业务线" class Contract(models.Model):
"""合同""" sn = models.CharField(u'合同号', max_length=128, unique=True)
name = models.CharField(u'合同名称', max_length=64)
memo = models.TextField(u'备注', blank=True, null=True)
price = models.IntegerField(u'合同金额')
detail = models.TextField(u'合同详细', blank=True, null=True)
start_date = models.DateField(blank=True)
end_date = models.DateField(blank=True)
license_num = models.IntegerField(u'license数量', blank=True)
create_date = models.DateField(auto_now_add=True)
update_date = models.DateField(auto_now=True) class Meta:
verbose_name = '合同'
verbose_name_plural = "合同" def __str__(self):
return self.name class IDC(models.Model):
"""机房""" name = models.CharField(u'机房名称', max_length=64, unique=True)
memo = models.CharField(u'备注', max_length=128, blank=True, null=True) def __str__(self):
return self.name class Meta:
verbose_name = '机房'
verbose_name_plural = "机房" class Tag(models.Model):
"""资产标签""" name = models.CharField('Tag name', max_length=32, unique=True)
creator = models.ForeignKey('UserProfile')
create_date = models.DateField(auto_now_add=True) def __str__(self):
return self.name class EventLog(models.Model):
"""事件""" name = models.CharField(u'事件名称', max_length=100)
event_type_choices = (
(1, u'硬件变更'),
(2, u'新增配件'),
(3, u'设备下线'),
(4, u'设备上线'),
(5, u'定期维护'),
(6, u'业务上线\更新\变更'),
(7, u'其它'),
)
event_type = models.SmallIntegerField(u'事件类型', choices=event_type_choices)
asset = models.ForeignKey('Asset')
component = models.CharField('事件子项', max_length=255, blank=True, null=True)
detail = models.TextField(u'事件详情')
date = models.DateTimeField(u'事件时间', auto_now_add=True)
user = models.ForeignKey('UserProfile', verbose_name=u'事件源')
memo = models.TextField(u'备注', blank=True, null=True) def __str__(self):
return self.name class Meta:
verbose_name = '事件纪录'
verbose_name_plural = "事件纪录" def colored_event_type(self):
if self.event_type == 1:
cell_html = '<span style="background: orange;">%s</span>'
elif self.event_type == 2:
cell_html = '<span style="background: yellowgreen;">%s</span>'
else:
cell_html = '<span >%s</span>'
return cell_html % self.get_event_type_display() colored_event_type.allow_tags = True
colored_event_type.short_description = u'事件类型' class NewAssetApprovalZone(models.Model):
"""新资产待审批区""" sn = models.CharField(u'资产SN号', max_length=128, unique=True)
asset_type_choices = (
('server', u'服务器'),
('switch', u'交换机'),
('router', u'路由器'),
('firewall', u'防火墙'),
('storage', u'存储设备'),
('NLB', u'NetScaler'),
('wireless', u'无线AP'),
('software', u'软件资产'),
('others', u'其它类'),
)
asset_type = models.CharField(choices=asset_type_choices, max_length=64, blank=True, null=True)
manufactory = models.CharField(max_length=64, blank=True, null=True)
model = models.CharField(max_length=128, blank=True, null=True)
ram_size = models.IntegerField(blank=True, null=True)
cpu_model = models.CharField(max_length=128, blank=True, null=True)
cpu_count = models.IntegerField(blank=True, null=True)
cpu_core_count = models.IntegerField(blank=True, null=True)
os_distribution = models.CharField(max_length=64, blank=True, null=True)
os_type = models.CharField(max_length=64, blank=True, null=True)
os_release = models.CharField(max_length=64, blank=True, null=True)
data = models.TextField(u'资产数据')
date = models.DateTimeField(u'汇报日期', auto_now_add=True)
approved = models.BooleanField(u'已批准', default=False)
approved_by = models.ForeignKey('UserProfile', verbose_name=u'批准人', blank=True, null=True)
approved_date = models.DateTimeField(u'批准日期', blank=True, null=True) def __str__(self):
return self.sn class Meta:
verbose_name = '新上线待批准资产'
verbose_name_plural = "新上线待批准资产"
models.py添加如下代码:
from django.contrib.auth.models import User class UserPofile(User):
''''''
name = models.CharField("姓名",max_length=32)
def __str__(self):
return self.name
初始化数据库
python3 manage.py makemigrations
python3 manage.py makemigrate
创建超级用户
python3 manage.py createsuperuser
admin.py里添加:
admin.site.register(models.Asset)
admin.site.register(models.Server)
admin.site.register(models.NetworkDevice)
admin.site.register(models.IDC)
admin.site.register(models.BusinessUnit)
admin.site.register(models.Contract)
admin.site.register(models.CPU)
admin.site.register(models.Disk)
admin.site.register(models.NIC)
admin.site.register(models.RAM)
admin.site.register(models.Manufactory)
admin.site.register(models.Tag)
admin.site.register(models.Software)
admin.site.register(models.EventLog)
为什么有中文有英文?
注释前代码
class Meta:
unique_together = ("asset", "slot") verbose_name = '硬盘'
verbose_name_plural = "硬盘"
注释掉
verbose_name = '硬盘'
verbose_name_plural = "硬盘"
看看web有什么变化
注释掉
verbose_name_plural = "硬盘"
看看web有什么变化
上面这就是Meta
是因为继承的原因
super(user.meate) python manage.py shell
from assets import models
models.Asset.objects.last().admin
models.Asset.objects.last().admin.user
- 先继承,后扩展
- 我继承的不是user表,AbstracUser.Meta
- 要继承这个什么都要自己写,太复杂了
CMDB资产管理系统开发【day25】:表结构设计2的更多相关文章
-
CMDB资产管理系统开发【day25】:需求分析
本节内容 浅谈ITIL CMDB介绍 Django自定义用户认证 Restful 规范 资产管理功能开发 浅谈ITIL TIL即IT基础架构库(Information Technology Infra ...
-
CMDB资产管理系统开发【day25】:windows客户端开发
1.目录结构 PS Y:\MadkingClient> tree /f 卷 netgame 的文件夹 PATH 列表 卷序列号为 ACE3-896E Y:. ├─bin │ NedStark.p ...
-
CMDB资产管理系统开发【day25】:表结构设计1
资产表 # _*_coding:utf-8_*_ __author__ = 'jieli' from assets.myauth import UserProfile from django.db i ...
-
CMDB资产管理系统开发【day25】:Django 自定义用户认证
官方文档:https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#substituting-a-custom-user-mode ...
-
CMDB资产管理系统开发【day26】:数据正式存入待存区
1.from表单提交 1.数据提交到哪里呢? 提交到assets/new_assets_approval.html这了 2.Yes, I'm sure提交了什么? 为什么没有下拉框了 ...
-
CMDB资产管理系统开发【day26】:CMDB上节回顾
一.上节知识点回顾 服务器设计了一个表结构 开发了一个客户端 二.后台创建缓存区表 客户端连接服务器,在服务器的下面看报错信息 因为URL都没有写,所以我找不到呀 1.在MadKing\url.py ...
-
CMDB资产管理系统开发【day26】:admin action
本节目标 审核写到数据库,我就单独写一个如下的 页面 单机go后就跳转到如下图界面,我们这节课的目标就是写一个这样的页面 asset\admin.py部分代码 注释如下: class NewAsset ...
-
CMDB资产管理系统开发【day26】:实现资产自动更新
1.需求分析 1.比对分析 比对的时候以那个数据源为主? old [1,2,3 ] db数据库 new [2,3,4 ] 客户端汇报过来的 当然以客户端汇报过来的数据为主 2.更新分析 不同的表到底拿 ...
-
CMDB资产管理系统开发【day27】:理解RESTful架构
理解RESTful架构 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(hig ...
随机推荐
-
Django框架学习
两个月前学习的Django框架,写了个简易婚恋调查网站,代码就懒得全贴了,有两张图记录下
-
Android 滑动效果基础篇(四)—— Gallery + GridView
Android系统自带一个GridView和Gallery两个控件,GridView网格显示,Gallery单个浏览,两者结合起来可以真正实现Gallery浏览图片效果. 本示例通过GridView和 ...
-
把 Eclipse 中的工程 Push 到 Github(适用 Windows 平台)
今天发现一小技巧,关于如何把Eclipse的某一个Existing project push 到github服务器. Eclipse 应该是 JavaEE 版本. 在project 右键 team, ...
-
Unity3D 相机跟随主角移动
这里给主相机绑定一个脚本. 脚本写为: using UnityEngine; using System.Collections; public class camerafollow : MonoBeh ...
-
1.Asp.net处理请求的流程
.NET平台处理HTTP请求的过程大致如下: 1. IIS得到一个请求: 2.查询脚本映射扩展,然后把请求映射到aspnet_isapi.dll文件 3.代码进入工作者进程(IIS5里是aspnet_ ...
-
从一般分布式设计看HDFS设计思想与架构
要想深入学习HDFS就要先了解其设计思想和架构,这样才能继续深入使用HDFS或者深入研究源代码.懂得了"所以然"才能在实际使用中灵活运用.快速解决遇到的问题.下面这篇博文我们就先 ...
-
(27)IO流小结
字节流 输入字节流: ---------| InputStream 所有输入字节流的基类. 抽象类 ------------| FileInputStream 读取文件的输入字节流 --------- ...
-
Python自学:第三章 确定列表长度
>>> cars = ["bmw", "audi", "toyota", "subaru"] > ...
-
mysql中的prepare介绍和应用
简单的用set或者declare语句定义变量,然后直接作为sql的表名是不行的,mysql会把变量名当作表名.在其他的sql数据库中也是如此,mssql的解决方法是将整条sql语句作为变量,其中穿插变 ...
-
NO--12模拟服务器端请求之node.js
最近几天项目上线,工作比较忙,没时间更博了,好在今天有点时间并且同事问道我一个问题,正好一块解决 使用 Vue 写项目肯定会遇到一个问题,如何模拟服务端请求数据,那这就需要用到 node.js 了. ...