使用一对一映射字段时,Django-Admin更改表单加载速度非常慢

时间:2016-05-26 08:11:31

标签: django django-forms django-admin

在我的情况下,更改表单加载非常慢在管理界面中,因为 models.py 中的 SeverHasDisk 模型中的one_to_one字段“disk” admin.py 中的 ServerHasDiskInLine 中有磁盘字段。当我从admin.py中的 ServerHasDiskInLine 中排除“磁盘”字段时,它可以正常工作。 有没有办法通过 ServerHasDiskInLine(admin.TabularInline)使用表单类来提高性能 或其他什么?

Models.py

#Models.py 

class Server(models.Model):

    hostname = models.CharField(max_length=64, unique=True, db_index=True)
    description = models.TextField(max_length=255, blank=True, null=True)
    note = models.TextField(max_length=255, blank=True, null=True)
    numberOfCpu = models.IntegerField(default=1)  # vCPU or HThread core
    cpuClockRate = models.CharField(max_length=255)  # Mhz
    virtualization = models.CharField(max_length=255, blank=True, null=True)
    ram = models.FloatField()  # Go
    vnc = models.IntegerField(blank=True, null=True, unique=True)
    monitored = models.BooleanField(default=True)
    dns = models.CharField(max_length = 255, blank=True, verbose_name= "DNS")
    ansible = models.CharField(max_length = 255, blank=True, verbose_name= "Ansible")
    # --------------Relations -----------------
    # backup = models.ForeignKey('self', name='server_backup', related_name='backup', blank=True, null=True)
    hypervisor = models.ForeignKey('self', name='server_hypervisor', related_name='hypervisor', blank=True, null=True)
    operatingSystem = models.ForeignKey(OperatingSystem)
    module = models.ForeignKey(Module, blank=True, null=True)
    project = models.ForeignKey(Project, blank=True, null=True)
    customer = models.ForeignKey(Customer, blank=True, null=True)
    environment = models.ForeignKey(Environment, blank=True, null=True)
    site = models.ForeignKey(Site, blank=True, null=True)
    status = models.ForeignKey(ServerStatus, blank=True, null=True )

    class Meta:
        ordering = ('hostname',)
        unique_together = ('hostname', 'site',)
class VolumeGroup(models.Model):

    name = models.CharField(max_length=64, unique=True, db_index=True)
    size = models.IntegerField()  # Go

    class Meta:
        ordering = ('name',)

    def __unicode__(self):
        return "%s (%s Go)" % (self.name, self.size)

class LogicalVolume(models.Model):
    # ------------------ Fields ---------------
    name = models.CharField(max_length=64, unique=True, db_index=True)
    size = models.IntegerField()  # Go
    note = models.TextField(max_length=255, blank=True, null=True)

    # --------------Relations -----------------
    volumeGroup = models.ForeignKey(VolumeGroup)

    class Meta:
        ordering = ('name',)
        unique_together = ('volumeGroup', 'name')

    def __unicode__(self):
        return "%s (%s Go) on VG <%s>" % (self.name, self.size, self.volumeGroup.name)

class ServerHasDisk(models.Model):
    # ------------------ Fields ---------------
    mountPoint = models.CharField(max_length=255)

    # --------------Relations -----------------
    server = models.ForeignKey(Server, related_name='serverDisks')
    disk = models.OneToOneField(LogicalVolume)

    class Meta:
        ordering = ('mountPoint',)
        unique_together = ('server', 'disk')

    def __unicode__(self):
        return "Srv [%s]: %s ==> <%s> on VG <%s>" % (self.server.hostname, self.mountPoint, self.disk.name, self.disk.volumeGroup.name)

Admin.py

class ServerHasDiskInline(admin.TabularInline):
    model = ServerHasDisk
    extra = 0
    # formset =  MyFormSet
    classes = ('grp-collapse grp-open',)
    #exclude = ['disk']



class ServerHasRoleInline(admin.TabularInline):
    model = ServerHasRole
    extra = 0
    fk_name = 'server'
    classes = ('grp-collapse grp-open',)
class IpInline(admin.TabularInline):
    model = IP
    extra = 0
    classes = ('grp-collapse grp-open',)
class BkSrvInline(admin.TabularInline):
    model = ServerIsBackupedOn
    fk_name = 'server'
    extra = 0
    classes = ('grp-collapse grp-open',)
class ServerAdmin(admin.ModelAdmin):
    change_list_template = "admin/change_list_filter_sidebar.html"
    list_display = ('hostname', 'get_project_name', 'get_module_name', 'get_environment_name', 'get_customer_name', 'get_os_name', 'get_site_name', 'get_ip_name', 'get_bk_hostname', 'get_role_name', 'description','note','get_dns','get_status','get_ansible')
    list_filter = ('project__name', 'module__name', 'environment__name', 'customer__name', 'site__name', 'operatingSystem__name', 'serverRoles__role__name', 'serverIps__address')
    list_per_page = 25
    search_fields = ('hostname', 'project__name', 'module__name', 'environment__name', 'serverIps__address', 'serverRoles__role__name', 'note')
    inlines = (ServerHasDiskInline,ServerHasVolumeGroupInline,ServerHasRoleInline, IpInline, BkSrvInline)
    #related_search_fields={
    #'disk':         ( 'name', ),

    #}

    def get_project_name(self, o):
        if o.project is None:
            return "N/A"
        else:
            return o.project.name
    get_project_name.admin_order_field = 'project'
    get_project_name.short_description = 'Project'
    def get_dns(self, o):
        if o.dns is None:
            return "N/A"
        else:
            return o.dns
    get_dns.admin_order_field = 'dns'
    get_dns.short_description = 'DNS'

    def get_ansible(self, o):
        if o.ansible is None:
            return "N/A"
        else:
            return o.ansible
    get_ansible.admin_order_field = 'ansible'
    get_ansible.short_description = 'Ansible'

    def get_module_name(self, o):
        if o.module is None:
            return "N/A"
        else:
            return o.module.name
    get_module_name.admin_order_field = 'module'
    get_module_name.short_description = 'Module'

    def get_customer_name(self, o):
        if o.customer is None:
            return "N/A"
        else:
            return o.customer.name
    get_customer_name.admin_order_field = 'customer'
    get_customer_name.short_description = 'Customer'


    def get_os_name(self, o):
        if o.operatingSystem is None:
            return "N/A"
        else:
            return o.operatingSystem.name
    get_os_name.admin_order_field = 'operatingsystem'
    get_os_name.short_description = 'OS'

    def get_environment_name(self, o):
        if o.environment is None:
            return "N/A"
        else:
            return o.environment.name
    get_environment_name.admin_order_field = 'environment'
    get_environment_name.short_description = 'Environment'
    def get_status(self, o):
        if o.status is None:
            return "N/A"
        else:
            return o.status.status
    get_status.admin_order_field = 'status'
    get_status.short_description = 'Status'
    def get_site_name(self, o):
        if o.site is None:
            return "N/A"
        else:
            return o.site.name
    get_site_name.admin_order_field = 'site'
    get_site_name.short_description = 'Site'

    def get_ip_name(self, o):
        return ", ".join([
            '<a href="%s/%s">%s</a>' % ('../ip', k.id, k.address)
            for k in o.serverIps.all()
        ])
        # return ", ".join([k.address for k in o.serverIps.all()])
    get_ip_name.admin_order_field = 'serverIps'
    get_ip_name.short_description = 'IP'
    get_ip_name.allow_tags = True

    def get_bk_hostname(self, o):
        return ", ".join([
            '<a href="%s/%s">%s</a>' % ('../server', k.backupServer.id, k.backupServer.hostname)
            for k in o.serverBackuped.all()
        ])

        # return '<a href="%s/%s">%s</a>' % ('../project', o.project.id, o.project.name)
    get_bk_hostname.admin_order_field = 'serverBackuped'
    get_bk_hostname.short_description = 'Bk Srv'
    get_bk_hostname.allow_tags = True

    def get_role_name(self, o):
        return ", ".join([k.role.name for k in o.serverRoles.all()])
    get_role_name.admin_order_field = 'serverRoles'
    get_role_name.short_description = 'Role'

1 个答案:

答案 0 :(得分:0)

这是快速解决方案。 我刚刚在 Admin.py 中为 ServerHasDiskInLine 类添加了一个属性 raw_id_fields =(“disk”,)所以新类是

class ServerHasDiskInline(admin.TabularInline):
    model = ServerHasDisk
    raw_id_fields = ("disk",)
    extra = 0
    classes = ('grp-collapse grp-open',)
    #exclude = ['disk']