2017-02-10 73 views
1

我想在HostInfo的管理顯示中顯示型號中的ip_address型號。django admin相關型號的顯示字段

# models.py 
class Hosts(models.Model): 
    host_name = models.CharField(max_length=200, unique=True) 
    ip_address = models.GenericIPAddressField(protocol='both', unpack_ipv4=True) 
    def __unicode__(self): 
    return unicode(self.host_name) 
    def hostip(self): 
    return unicode(self.ip_address) 

,我有以下的admin.py

# admin.py 
class HostInfoResource(resources.ModelResource): 

    host = fields.Field(column_name='host', 
           attribute='host', 
           widget=ForeignKeyWidget(Hosts, 'host_name')) 
    project = fields.Field(column_name='project', 
           attribute='project', 
           widget=ForeignKeyWidget(Project, 'project_name')) 
    env = fields.Field(column_name='env', 
          attribute='env', 
          widget=ForeignKeyWidget(Env, 'env_name')) 

    class Meta: 
     model = HostInfo 
     skip_unchanged = True 
     import_id_fields = ('id', 'host','ticket','deployed_by') 
     export_order = ('id', 'host', 'nexpose_level','cpus','memory','os', 
         'sudoers_copied', 'sudo_granted', 'extra_disks','app_type','app_name', 
         'vcenter_status','ticket','env','project','deployed_by', 
         'updated_on','created_on') 

class HostInfoAdmin(ImportExportModelAdmin): 
    resource_class = HostInfoResource 
    list_display = ['id', 'host', 'nexpose_level','cpus','memory','os', 
         'sudoers_copied', 'sudo_granted', 'extra_disks','app_type','app_name', 
         'vcenter_status','ticket','env','project','deployed_by'] 

    readonly_fields = ('updated_on','created_on',) 

admin.site.register(HostInfo, HostInfoAdmin) 

我不是很理解我需要什麼來實現這個目的。

回答

1

嘗試使用雙下劃線當你想要嵌套關係。

class HostInfoAdmin(ImportExportModelAdmin): 
    resource_class = HostInfoResource 
    list_display = ['id','host__ip_address', 'host', 'nexpose_level','cpus','memory','os', 
         'sudoers_copied', 'sudo_granted', 'extra_disks','app_type','app_name', 
         'vcenter_status','ticket','env','project','deployed_by'] 

    def host__ip_address(self, obj): 
     return obj.host.ip_address 
+0

<類 'inventory.admin.HostInfoAdmin'>:(admin.E108)的 'list_display [1]' 是指 'host__ip_address',這是不可調用,一個的 'HostInfoAdmin' 屬性的值,或'inventory.HostInfo'上的屬性或方法。 –

+0

檢查它可能有幫助http://stackoverflow.com/questions/32883057/foreign-key-reference-for-django-in-admin – Darshan

+1

該鏈接得到我需要的答案。謝謝。 –

相關問題