如何忽略在django admin list_display中加载巨大的字段?(How to ignore loading huge fields in django admin list_display?)

我正在使用django 1.9和django.contrib.gis以及具有巨大gis MultiPolygonField的Area模型:

# 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 :

# 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不包含mpoly ,而exclude属性阻止它显示在窗体视图中,但在显示列表视图时,它仍然从数据库中提取所有字段并将其加载到内存中。 由于mpoly非常庞大,我遇到了随机错误(段错误,处理死亡,...),并且列表显示需要很多分钟来显示一些简单的整数字段......

有没有什么办法可以告诉django不要将mpoly加载到内存中,在数据库查询中完全忽略它,以便加载速度更快? 除了exclude外,我还没有发现文档中的任何内容以远程实现此目的。 我问这里,以防我错过了什么。

谢谢您的帮助。

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'

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)

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...

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.

Thank you for your assistance.

最满意答案

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

class AreaAdmin(gis_admin.OSMGeoAdmin): def get_queryset(self, request): qs = super(AreaAdmin, self).get_queryest(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

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_queryest(request) # tell Django to not retrieve mpoly field from DB qs = qs.defer('mpoly') return qs

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

更多推荐