一、定义数据模型
[root@localhost simplecmdb]# cat hostinfo/models.py from django.db import models # Create your models here. class Host(models.Model): hostname = models.CharField(max_length=50) ip = models.IPAddressField() vendor = models.CharField(max_length=50) product = models.CharField(max_length=50) sn = models.CharField(max_length=50) cpu_model =models.CharField(max_length=50) cpu_num = models.IntegerField() memory = models.CharField(max_length=50) osver = models.CharField(max_length=50) def __unicode__(self): # 重新改写unicode return self.hostname class HostGroup(models.Model): # 定义数据模型 groupname = models.CharField(max_length=50) members = models.ManyToManyField(Host)
[root@localhost simplecmdb]# python manage.py validate # 检查语法
[root@localhost simplecmdb]# python manage.py syncdb # 同步数据
二、注册数据模型
[root@localhost simplecmdb]# vim hostinfo/admin.py from django.contrib import admin from hostinfo.models import Host, HostGroup # 导入数据模型 # Register your models here. class HostAdmin(admin.ModelAdmin): list_display = [ ‘hostname‘, ‘ip‘, ‘cpu_model‘, ‘cpu_num‘, ‘memory‘, ‘vendor‘, ‘product‘, ‘osver‘, ‘sn‘ ] class HostGroupAdmin(admin.ModelAdmin): # 定义要显示在Web界面的字段 list_display = [‘groupname‘] admin.site.register(Host, HostAdmin) admin.site.register(HostGroup, HostGroupAdmin) # 注册数据模型
三、在 Web 界面设置主机组
如下,表示将 node01 、node02 、node03 归为 web 组