CMDB实现数据展示功能部分代码。 views.py def asset(request): asset = Asset.objects.all() return render_to_response("asset.html",{'asset': asset}) models.py class Asset(models.Model): ipaddr = models.CharField(max_length=20, verbose_name=u'IP地址', primary_key=True) #需定义primary_key,否则会自动创建id列 hostname = models.CharField(max_length=30, verbose_name=u'主机名') nodes = models.CharField(max_length=20, verbose_name=u'应用节点') applications = models.CharField(max_length=30, verbose_name=u'应用名') group = models.CharField(max_length=20, verbose_name=u'应用组名') memory = models.CharField(max_length=5, verbose_name='内存大小') env = models.CharField(max_length=5, verbose_name='环境变量') flag = models.PositiveSmallIntegerField(verbose_name=u'flag') #def __unicode__(self): # return u'%s,%s,%s,%s,%s,%s,%s,%s' %(self.ipaddr, self.hostname, self.nodes, self.app, self.group, self.memory, self.disk, self.cpu) class Meta: db_table = 'fcenv_copy' #表名,默认为app名+类名 verbose_name = u'主机资产信息' verbose_name_plural = u'主机资产信息管理' asset.html <table class="table table-striped table-bordered table-hover " id="editable" name="editable"> <thead class="fixedThead"> <tr> <!-- <th class="text-center"> <input id="checkall" type="checkbox" class="i-checks" name="checkall" value="checkall" data-editable="false" onclick="check_all('asset_form')"> </th> --> <th class="text-center" name="ip"> IP地址 </th> <th class="text-center" width="80px"> 机器名 </th> <th class="text-center"> 节点 </th> <th class="text-center" width="300px"> 应用名称 </th> <th class="text-center" width="100px"> 组别 </th> <th class="text-center"> 内存 </th> <th class="text-center" width="200px"> 环境变量 </th> <th class="text-center"> flag </th> <th class="text-center" width="200px"> 操作 </th> </tr> </thead> <tbody class="scrollTbody"> {% for value in asset %} <tr class="gradeX"> <td class="text-center">{{ value.ipaddr }}</a></td> <td class="text-center hostname">{{ value.hostname }}</a></td> <td class="text-center">{{ value.nodes }}</a></td> <td class="text-center">{{ value.applications }}</a></td> <td class="text-center">{{ value.group }}</a></td> <td class="text-center">{{ value.memory }}</a></td> <td class="text-center">{{ value.env }}</a></td> <td class="text-center">{{ value.flag }}</a></td> <td class="text-center" data-editable="false"> <a href="#" class="btn btn-xs btn-info">编辑</a> <a value="#" class="conn btn btn-xs btn-warning">连接</a> <a value="#" class="btn btn-xs btn-danger asset_del">删除</a> </td> {% endfor %} </tr> </tbody> </table>
以下是常用ORM操作:
# models.Tb1.objects.create(c1='xx', c2='oo') 增加一条数据,可以接受字典类型数据 **kwargs
# obj = models.Tb1(c1='xx', c2='oo')
# obj.save()
# 查
#
# models.Tb1.objects.get(id=123) # 获取单条数据,不存在则报错(不建议)
# models.Tb1.objects.all() # 获取全部
# models.Tb1.objects.filter(name='seven') # 获取指定条件的数据
# 删
#
# models.Tb1.objects.filter(name='seven').delete() # 删除指定条件的数据
# 改
# models.Tb1.objects.filter(name='seven').update(gender='0') # 将指定条件的数据更新,均支持 **kwargs
# obj = models.Tb1.objects.get(id=1) # 修改单条数据
# obj.c1 = '111'
# obj.save()
# 获取个数
#
# models.Tb1.objects.filter(name='seven').count()
# 大于,小于
#
# models.Tb1.objects.filter(id__gt=1) # 获取id大于1的值
# models.Tb1.objects.filter(id__lt=10) # 获取id小于10的值
# models.Tb1.objects.filter(id__lt=10, id__gt=1) # 获取id大于1 且 小于10的值
# in
#
# models.Tb1.objects.filter(id__in=[11, 22, 33]) # 获取id等于11、22、33的数据
# models.Tb1.objects.exclude(id__in=[11, 22, 33]) # not in
# contains
#
# models.Tb1.objects.filter(name__contains="ven")
# models.Tb1.objects.filter(name__icontains="ven") # icontains大小写不敏感
# models.Tb1.objects.exclude(name__icontains="ven")
# range
#
# models.Tb1.objects.filter(id__range=[1, 2]) # 范围bettwen and
# 其他类似
#
# startswith,istartswith, endswith, iendswith,
# order by
#
# models.Tb1.objects.filter(name='seven').order_by('id') # asc
# models.Tb1.objects.filter(name='seven').order_by('-id') # desc
# limit 、offset
#
# models.Tb1.objects.all()[10:20]
# group by
from
django.db.models
import
Count,
Min
,
Max
,
Sum
# models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num'))
# SELECT "app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"
Many-to-one relationships 多表关联查询。
from django.db import models class Reporter(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) email = models.EmailField() def __str__(self): # __unicode__ on Python 2 return "%s %s" % (self.first_name, self.last_name) class Article(models.Model): headline = models.CharField(max_length=100) pub_date = models.DateField() reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE) def __str__(self): # __unicode__ on Python 2 return self.headline class Meta: ordering = ('headline',) >>> r.article_set.filter(headline__startswith='This') <QuerySet [<Article: This is a test>]># Find all Articles for any Reporter whose first name is "John". >>> Article.objects.filter(reporter__first_name='John') <QuerySet [<Article: John's second story>, <Article: This is a test>]>
效果图。
附件是样式文件。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。