基于django类的列表视图中的多对多select_related

时间:2014-08-11 14:49:29

标签: django orm

我们有一个示例模型:

#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查询。

1 个答案:

答案 0 :(得分:6)

M2M使用prefetch_related而非select_related。 修复模型(有不同的方法来做你想做的事):

你的模特:

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 %}