基于Django类的列表视图中的多对多select_related [英] many-to-many select_related in django class-based list view

查看:81
本文介绍了基于Django类的列表视图中的多对多select_related的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个示例模型:

#models.py
class Author(models.Model):
    name = models.CharField(max_length=255)

class Book(models.Model):
    name = models.CharField(max_length=255)
    author = models.ManyToManyField(Author, blank=True)

    def get_authors(self):
        return self.authors.all().order_by('id').values_list('name')

#views.py
class BooksView(ListView):
    model = Book

    def get_queryset(self):
        q = Book.select_related('authors').all()


#template
{% for book in books %}

    {{ book.name }} ({%for author in book.get_authors %} {{ author }} {% endfor %}

{% endfor %}

当我尝试使用get_authors函数从模板获取数据时,我看到多个SQL查询会极大地降低性能(SQL的工作时间约为5秒).是否可以减少查询?现在,我看到每个周期中每个作者的SQL查询.

When I try to get data from template using get_authors function, I see multiple SQL queries that dramatically reduce performance (SQL works about 5sec). Is it possible to reduce queries? Now I see SQL query for each author in cycle.

推荐答案

M2M使用与select_related不相关的prefetch_related. 修复模型(有多种方法可以完成您想做的事情):

M2M uses prefetch_related not select_related. Fix the Model (there are different ways to do what you want to do):

您的型号:

class Book(models.Model):
    name = models.CharField(max_length=255)
    author = models.ManyToManyField(Author, blank=True)

    def get_authors(self):
        if self.authors:
            return '%s' % " / ".join([author.name for author in self.authors.all()])

修正您的观点:

class BooksView(ListView):
    """
    Note that default Django views use object_list for the context,
    in order to use books, you need to define context_object_name.
    """
    context_object_name = "books"
    """
    You don't need to override the queryset for this kind of operation.
    Just define the queryset attribute of the CBV.
    """
    queryset = Book.objects.prefetch_related('authors')

然后在您的模板中:

#template
{% for book in books %}

    {{ book.name }} {{ book.get_authors }}

{% endfor %}

这篇关于基于Django类的列表视图中的多对多select_related的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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