如何忽略在Django管理员list_display中加载大字段? [英] How to ignore loading huge fields in django admin list_display?

查看:83
本文介绍了如何忽略在Django管理员list_display中加载大字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将django 1.9和django.contrib.gis与具有巨大gis MultiPolygonFieldArea模型一起使用:

I'm using django 1.9 and django.contrib.gis with an Area model that has a huge gis MultiPolygonField:

# models.py
from django.contrib.gis.db import models as gis_models

class Area(gis_models.Model):

    area_color = gis_models.IntegerField()
    mpoly = gis_models.MultiPolygonField(srid=4326)

    class Meta:
        verbose_name = 'Area'
        verbose_name_plural = 'Areas'

我有相关的AreaAdmin类来管理django admin中的Area:

I have the associated AreaAdmin class to manage the Areas inside django admin:

# admin.py
from django.contrib.gis import admin as gis_admin

class AreaAdmin(gis_admin.OSMGeoAdmin):
    map_width = 800
    map_height = 600
    modifiable = False
    list_display = ['area_color', ]
    exclude = ['mpoly', ]
gis_admin.site.register(Area, AreaAdmin)

问题是,即使我使用的list_display不包含mpolyexclude属性,以防止其显示在表单视图中,当显示列表视图时,它仍会获取所有数据库中的字段并将其加载到内存中.由于mpoly是如此之大,所以我遇到了随机错误(段错误,已处理终止,...),并且列表显示需要花费几分钟才能显示一些简单的整数字段...

The problem is that even though I'm using list_display that doesn't contain mpoly and the exclude attribute to prevent it's display in the form view , when displaying the list view, it still fetches all the fields from the database and loads it into memory. Because the mpoly is so huge, I'm having random errors (segfault, processed killed, ...) and the list display takes many minutes to display some simple integer fields...

是否有任何方法告诉django不要将mpoly加载到内存中,以完全忽略它在数据库查询中,以便快速加载?除了exclude之外,我没有在文档中找到任何可以远程实现此目标的东西.我在这里问,以防万一我丢失了一些东西.

Is there any way to tell django not to load the mpoly into memory, to ignore it completely in it's database query so it will load fast ? I haven't found anything in the documentation aside from exclude to remotely achieve this. I'm asking here in case I'm missing something.

谢谢您的帮助.

推荐答案

您可以尝试覆盖为AreaAdmin生成列表视图时使用的get_queryset方法.

You can try to override the get_queryset method used in generating the list view for AreaAdmin.

class AreaAdmin(gis_admin.OSMGeoAdmin):

    def get_queryset(self, request):
        qs = super(AreaAdmin, self).get_queryset(request)

        # tell Django to not retrieve mpoly field from DB
        qs = qs.defer('mpoly') 
        return qs

有关defer的更多信息,请参见 https://docs.djangoproject.com/es/1.9/ref/models/querysets/#defer

For more information on defer see https://docs.djangoproject.com/es/1.9/ref/models/querysets/#defer

这篇关于如何忽略在Django管理员list_display中加载大字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆