Django-预取的使用

Django-预取的使用,django,django-models,Django,Django Models,无论我读了多少教程/文档,我仍然不太明白我到底应该如何使用预回迁 My models.py: class ProfileComment(models.Model): author = models.ForeignKey('Profile', on_delete=models.CASCADE, null=True) date_posted = models.DateTimeField(default=timezone.now, editable=False)

无论我读了多少教程/文档,我仍然不太明白我到底应该如何使用预回迁

My models.py:

class ProfileComment(models.Model):
    author       = models.ForeignKey('Profile', on_delete=models.CASCADE, null=True)
    date_posted  = models.DateTimeField(default=timezone.now, editable=False)
    body         = models.CharField(max_length=180, blank=True)
    ...

class Profile(models.Model):
    user     = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    comments = models.ManyToManyField(ProfileComment, related_name='comments', blank=True)
    avatar   = models.FileField(upload_to=avatar_folder, default='user-avatar/default.png')
    ...
My views.py:

profile = Profile.objects.prefetch_related('comments').get(user=request.user)
在模板中:

{% for comment in profile.comments.all %}
<div>
  <p>Author: {{ comment.author.user }}</p><img src="{{ comment.author.avatar.url }}">
  <p>Message: {{ comment.body }}</p>
  <p>Date posted: {{ comment.date_posted }}</p>
</div>
{% endfor %}
{%用于profile.comments.all%中的注释]
作者:{{comment.Author.user}

消息:{comment.body}

发布日期:{comment.Date_posted}

{%endfor%}
然而,无论我在prefetch_related中添加了什么,每个记录的查询量都会增加5个,应该使用它来批量获取查询集的相关对象。既然你写了:

.get(user=request.user)
概要文件
注释
传递给模板,然后使用以下内容检索模板:

{% for comment in comments %}
<div>
  <p>Author: {{ comment.author.user }}</p><img src="{{ comment.author.avatar.url }}">
  <p>Message: {{ comment.body }}</p>
  <p>Date posted: {{ comment.date_posted }}</p>
</div>
{% endfor %}
{%用于注释%中的注释]
作者:{comment.Author.user}

.Author.avatar.url}> 消息:{comment.body}

发布日期:{comment.Date_posted}

{%endfor%}
在这里,我们从一个查询集中读取注释,在同一个查询中获取配置文件和该配置文件的用户。因此,我们将进行两个查询:一个查询
配置文件
,另一个查询所有
注释
,包括
。author
。author.user
应用于获取相关对象ts用于批量查询集。因为您编写了:

.get(user=request.user)
概要文件
注释
传递给模板,然后使用以下内容检索模板:

{% for comment in comments %}
<div>
  <p>Author: {{ comment.author.user }}</p><img src="{{ comment.author.avatar.url }}">
  <p>Message: {{ comment.body }}</p>
  <p>Date posted: {{ comment.date_posted }}</p>
</div>
{% endfor %}
{%用于注释%中的注释]
作者:{comment.Author.user}

.Author.avatar.url}> 消息:{comment.body}

发布日期:{comment.Date_posted}

{%endfor%}

因此,这里我们从查询集中读取注释,在同一查询中获取概要文件和该概要文件的用户。因此,我们将进行两个查询:一个查询
配置文件
,另一个查询所有
评论
,包括
.author
.author。用户
预回迁相关的
将进行额外查询。对于一个
.get(user=request.user)
它不会有任何区别。那么我如何才能使它不为一条记录再进行10次查询呢?目前我有10条记录,需要80次查询才能加载它们。你没有
.prefetch\u related
进行额外查询,以最小化数据库的带宽。如果要在同一个查询中获取所有注释,请在每个记录中重复配置文件的数据。如果要为额外的关系执行此操作,则需要重复配置文件的数据乘以行数,注释的数据乘以其他关系。这是因为
comment.author.user
部分。可以使用
进行优化。选择\u related
Prefetch\u related
将进行额外查询。对于一个
.get(user=request.user)
它不会有任何区别。那么我如何才能使它不为一条记录再进行10次查询呢?目前我有10条记录,需要80次查询才能加载它们。你没有
.prefetch\u related
进行额外查询,以最小化数据库的带宽。如果要在同一个查询中获取所有注释,请在每个记录中重复配置文件的数据。如果要为额外的关系执行此操作,则需要重复配置文件的数据乘以行数,注释的数据乘以其他关系。这是因为
comment.author.user
部分。这可以通过
进行优化。选择\u related
。谢谢,这确实有助于消除混淆谢谢,这确实有助于消除混淆