您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。