您好,登錄后才能下訂單哦!
CMDB實(shí)現(xiàn)數(shù)據(jù)展示功能部分代碼。 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,否則會自動(dòng)創(chuàng)建id列 hostname = models.CharField(max_length=30, verbose_name=u'主機(jī)名') nodes = models.CharField(max_length=20, verbose_name=u'應(yīng)用節(jié)點(diǎn)') applications = models.CharField(max_length=30, verbose_name=u'應(yīng)用名') group = models.CharField(max_length=20, verbose_name=u'應(yīng)用組名') memory = models.CharField(max_length=5, verbose_name='內(nèi)存大小') env = models.CharField(max_length=5, verbose_name='環(huán)境變量') 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' #表名,默認(rèn)為app名+類名 verbose_name = u'主機(jī)資產(chǎn)信息' verbose_name_plural = u'主機(jī)資產(chǎn)信息管理' 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"> 機(jī)器名 </th> <th class="text-center"> 節(jié)點(diǎn) </th> <th class="text-center" width="300px"> 應(yīng)用名稱 </th> <th class="text-center" width="100px"> 組別 </th> <th class="text-center"> 內(nèi)存 </th> <th class="text-center" width="200px"> 環(huán)境變量 </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') 增加一條數(shù)據(jù),可以接受字典類型數(shù)據(jù) **kwargs
# obj = models.Tb1(c1='xx', c2='oo')
# obj.save()
# 查
#
# models.Tb1.objects.get(id=123) # 獲取單條數(shù)據(jù),不存在則報(bào)錯(cuò)(不建議)
# models.Tb1.objects.all() # 獲取全部
# models.Tb1.objects.filter(name='seven') # 獲取指定條件的數(shù)據(jù)
# 刪
#
# models.Tb1.objects.filter(name='seven').delete() # 刪除指定條件的數(shù)據(jù)
# 改
# models.Tb1.objects.filter(name='seven').update(gender='0') # 將指定條件的數(shù)據(jù)更新,均支持 **kwargs
# obj = models.Tb1.objects.get(id=1) # 修改單條數(shù)據(jù)
# obj.c1 = '111'
# obj.save()
# 獲取個(gè)數(shù)
#
# 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的數(shù)據(jù)
# 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 多表關(guān)聯(lián)查詢。
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>]>
效果圖。
附件是樣式文件。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。