覆盖get_queryset方法后,Django管理员列表页面将永久加载 [英] Django admin list page takes forever to load after overriding get_queryset method

查看:130
本文介绍了覆盖get_queryset方法后,Django管理员列表页面将永久加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个模型管理员-

class NewsAdmin(ImageWidgetAdmin):
    image_fields = ['featured_image']
    list_per_page = 20
    list_display = ('heading', 'category', 'status', 'is_active', 'created_at', 'published_at',
                    'created_by', 'published_by')
    list_editable = ('category', 'status', 'is_active')
    list_filter = ('published_at', 'created_at', 'status', 'is_active', 'created_by',
                   'published_by',)
    search_fields = ('heading', 'category', 'tags', 'source')
    actions = [enable_object, disable_object, status_draft, status_private, status_public]
    actions_on_bottom = True

加载最多需要400毫秒。这是django-debug-toolbar的图片-

It only takes max 400ms to load. Here's the django-debug-toolbar image -

没有get_queryset的djdt图像

但是当我为语言过滤后的对象覆盖get_queryset方法时-

But when I override the get_queryset method for language filtered objects -

    def get_queryset(self, request):
        queryset = super(NewsAdmin, self).get_queryset(request)
        return queryset.filter(language=request.LANGUAGE_CODE)

这大约需要17-18秒,真是太疯狂了!
这是django-debug-toolbar图像-

It takes around 17-18 seconds which is nuts!! Here's the django-debug-toolbar image -

带有get_queryset的djdt图像

即使对于前端查询也是如此!有关详细信息-我有大约40万条记录的数据库表,这是模型-

Even same this is happening for the front end queries as well! For details - I have database table with around 400k records and here's the model -

class News(BaseEntityBasicAbstract, HitCountMixin):
    NEWS_STATUS = (
        ('draft', _('Draft')),
        ('pending', _('Pending')),
        ('review', _('Review')),
        ('public', _('Public')),
        ('private', _('Private'))
    )
    backup = models.BooleanField(default=False)
    prev_id = models.BigIntegerField(null=True, blank=True)
    language = models.CharField(max_length=10, choices=LANGUAGES, default='bn')
    heading = models.CharField(max_length=255, null=True, blank=True,
                               verbose_name=_('News Heading'),
                               help_text=_('Provide a news heading/caption.'))
    sub_caption = models.TextField(max_length=255, null=True, blank=True,
                                   verbose_name=_('Summary'),
                                   help_text=_('Provide summary of the news.'))
    url = models.CharField(max_length=255, unique=True, verbose_name=_('URL/Slug/Link'),
                           help_text=_('Unique url for the news without whitspace.'))
    content = HTMLField(null=True, blank=True, verbose_name=_('Content'),
                        help_text=_('HTML content with texts, links & images.'))
    featured_image = models.FileField(upload_to=FilePrefix('news/'), null=True, blank=True,
                                      verbose_name=_('Featured Image'),
                                      help_text=_('Upload a featured image for news.'))
    image_caption = models.TextField(max_length=255, null=True, blank=True,
                                     verbose_name=_('Image Caption'),
                                     help_text=_('Provide a image caption.'))
    status = models.CharField(max_length=20, choices=NEWS_STATUS, default='pending',
                              verbose_name=_('News Status'),
                              help_text=_('Only public news can be seen on front end.'))
    source = models.ForeignKey(NewsSource, on_delete=models.SET_NULL, null=True, blank=True,
                               verbose_name=_('News Source'),
                               help_text=_('Select a news source.'))
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True,
                                 verbose_name=_('Category'),
                                 help_text=_('Select a news category.'))
    tags = tagulous.models.TagField(
        blank=True,
        to=Tags,
        verbose_name=_('News Tags'),
        help_text=_('Provide news tags separated with commas.')
    )
    published_at = models.DateTimeField(null=True, blank=True,
                                        verbose_name=_('Published At'))
    menu_items = GenericRelation(MenuItems, object_id_field='id',
                                 related_query_name='news_as_menu')
    hit_count_generic = GenericRelation(HitCount, object_id_field='object_pk',
                                        related_query_name='news_hit_count')
    created_by = models.ForeignKey(User, related_name='news_created_by',
                                   on_delete=models.SET_NULL, null=True, blank=True,
                                   verbose_name=_('Created By'))
    updated_by = models.ForeignKey(User, related_name='news_updated_by',
                                   on_delete=models.SET_NULL, null=True, blank=True,
                                   verbose_name=_('Last Updated By'))
    published_by = models.ForeignKey(User, related_name='news_published_by',
                                     on_delete=models.SET_NULL, null=True, blank=True,
                                     verbose_name=_('Published By'))
    deleted_by = models.ForeignKey(User, related_name='news_deleted_by',
                                   on_delete=models.SET_NULL, null=True, blank=True,
                                   verbose_name=_('Deleted By'))

我不知道为什么会这样!请帮助我在这里解决问题!

I'm lost why is this happening! Please help me figure out the problem here!

推荐答案

您正在过滤没有索引的字段。表越大,数据库需要扫描每一行所花费的时间就越长。更改字段定义以允许使用索引并进行迁移。

You are filtering on a field without an index. The bigger the table, the longer it takes as the database needs to scan each row. Alter your field definition to allow for an index and take care of migrations.

language = models.CharField(max_length=10, choices=LANGUAGES, default='bn' db_index=True)

这篇关于覆盖get_queryset方法后,Django管理员列表页面将永久加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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