5 Django系列之通过list_display展示多对多与外键内容在admin-web界面下

时间:2023-01-01 12:43:45

list_display展示多对多关系的内容

表结构关系

表一

class Server(models.Model):
asset = models.OneToOneField('Asset')
created_by_choices = (
('auto','Auto'),
('manual','Manual'),
)
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
model = models.CharField(u'型号',max_length=128,null=True, blank=True )
# 若有多个CPU,型号应该都是一致的,故没做ForeignKey
raid_type = models.CharField(u'raid类型',max_length=512, 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)
salt_minion_id = models.CharField(u'salt minion id',max_length=254,blank=True,null=True)
create_date = models.DateTimeField(blank=True, auto_now_add=True)
update_date = models.DateTimeField(blank=True,null=True)
class Meta:
verbose_name = '服务器'
verbose_name_plural = "服务器"
def __str__(self):
return '%s sn:%s' %(self.asset.name,self.asset.sn)

表二

class SaltGroup(models.Model):
'''
saltstack 的minion组管理
'''
group_name = models.CharField(u'组名',max_length=50)
host_target = models.ManyToManyField(Server,verbose_name='组成员',null=True,blank=True)
whether_create = models.IntegerField(u'是否创建了组在saltstack配置文件里',default=0) #0:没有创建,1:表示创建了
class Meta:
db_table = "saltstack组"

然后我们在admin配置文件这么写:

class Saltstack_GroupAdmin(admin.ModelAdmin):
list_display = ['group_name','salt_minion_id'] # 这里需要注意的是,salt_minion_id是我们要在前端展示的内容,自己定义的
def salt_minion_id(self,obj):
'''
这个方法就是用来对salt_minion_id这个字段做处理,把我们需要展示的前端内容截取出来。需要注意的是,方法名必须要和在list_display里面的一致,这样才可以调用。
'''
print(obj.host_target.salt_minion_id)
return obj.host_target.salt_minion_id # host_target是我们SaltGroup表中多对多的字段,salt_minion_id是刚才我们自定义的字段,
salt_minion_id.short_description = "Minion's ID " # 对salt_minion_id这个做个简短的title。 admin.site.register(models.SaltGroup,Saltstack_GroupAdmin),

好了至此,重启下django即可在django admin界面下使用了

list_display展示外键内容

首先看看表结构关系

表一

class DockerContainers(models.Model):
'''
用来记录docker容器信息的
'''
Real_host_ip = models.ForeignKey(DockerOfHost,verbose_name='Docker宿主机IP') # 这段是外键
Container_id = models.CharField(u'容器ID',max_length=400,primary_key=True,unique=True)
Container_name = models.CharField(u'容器名字',max_length=300,null=True,blank=True)
Container_image = models.CharField(u'容器镜像',max_length=300)
Container_Image_id = models.CharField(u'镜像ID',max_length=500,null=True,blank=True)
Command = models.CharField(u'运行的命令',max_length=300,null=True,blank=True)
Created = models.CharField(u'创建时间',max_length=200)
Status = models.CharField(u'运行状态',max_length=100,null=True,blank=True)
Port = models.CharField(u'容器端口',max_length=1000,null=True,blank=True)
SizeRw = models.IntegerField(null=True,blank=True)
SizeRootFs = models.IntegerField(null=True,blank=True)
Host_config = models.CharField(u'主机配置',max_length=1024,null=True,blank=True)
Network_settings = models.CharField(u'网络配置',max_length=3000,null=True,blank=True)
Mounts = models.CharField(u'挂载目录',max_length=1024,null=True,blank=True)
Record_time = models.DateTimeField(u'数据更新时间',auto_now=True) def __str__(self):
return "%s:%s"%(self.Real_host_ip,self.Container_id) class Meta:
verbose_name = "Docker容器信息"
verbose_name_plural = "Docker容器信息"

表二:

class DockerOfHost(models.Model):
'''
用来记录docker宿主机信息到
'''
host_ip = models.CharField(u'Docker宿主机IP',max_length=250,unique=True)
def __str__(self):
return self.host_ip # 一定要retrun self.ip,这样list_display才能显示
class Meta:
verbose_name = "Docker宿主机IP"
verbose_name_plural = "Docker宿主机IP"

admin.py里的代码如下:

class dockercontainers(admin.ModelAdmin):
list_display = ['Real_host_ip','Container_id','Container_name','Container_image','Container_Image_id','Command','Created','Status',\
'Port','SizeRw','SizeRootFs','Host_config','Network_settings','Mounts','Record_time']
search_fields = ['Container_id','Container_name','Container_image','Status']

其实最关键的一步在于表二里的__str__方法return回来的内容,套用官网的一句话就是:If the field is a ForeignKey, Django will display the str() (unicode() on Python 2) of the related object. If you don’t set list_display, the admin site will display a single column that displays the str() (unicode() on Python 2) representation of each object.

更多资料可以参考官网:https://docs.djangoproject.com/en/1.10/ref/contrib/admin/