gpt4 book ai didi

django - 减少Django中的数据库查询

转载 作者:行者123 更新时间:2023-12-04 13:12:14 24 4
gpt4 key购买 nike

我有一个 View ,可搜索电影信用数据库,并转换并返回结果,如下所示:

# From the following results:
Avatar - James Cameron - director
Avatar - James Cameron - writer
Avatar - James Cameron - editor
Avatar - Julie Jones - writer
Crash - John Smith - director

# ...display in the template as:
Avatar - James Cameron (director, writer, editor)
Avatar - Julie Jones (writer)
Crash - John Smith (director)

但是,当我执行此转换并执行 print connection.queries时,我访问数据库的次数大约为100次。这是我目前拥有的-
# in models
class VideoCredit(models.Model):
video = models.ForeignKey(VideoInfo)

# if the credit is a current user, FK to his profile,
profile = models.ForeignKey('UserProfile', blank=True, null=True)
# else, just add his name
name = models.CharField(max_length=100, blank=True)
# normalize name for easier searching / pulling of name
normalized_name = models.CharField(max_length=100)

position = models.ForeignKey(Position)
timestamp = models.DateTimeField(auto_now_add=True)
actor_role = models.CharField(max_length=50, blank=True)

class VideoInfo(models.Model):
title = models.CharField(max_length=256, blank=True)
uploaded_by = models.ForeignKey('UserProfile')
...

类Position(models.Model):
位置= models.CharField(max_length = 100)
排序= models.IntegerField(max_length = 3)
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
...

我认为,我正在以 (name, video, [list_of_positions])的形式构建一个三元组列表,以显示信用额-
    credit_set = VideoCredit.objects.filter(***depends on a previous function***)
list_of_credit_tuples = []
checklist = [] # I am creating a 'checklist' to see whether to append the positions
# list of create a new tuple entry
for credit in credit_set:
if credit.profile: # check to see if the credit has an associated profile
name = credit.profile
else:
name = credit.normalized_name
if (credit.normalized_name, credit.video) in checklist:
list_of_keys = [(name, video) for name, video, positions in list_of_credit_tuples]
index = list_of_keys.index((name, credit.video))
list_of_credit_tuples[index][2].append(credit.position)
else:
list_of_credit_tuples.append((name, credit.video, [credit.position]))
checklist.append((credit.normalized_name, credit.video))
...

最后,在我的模板中显示信用额度(注意:如果信用额度具有个人资料,请提供指向用户个人资料的链接)-
{% for name, video, positions in list_of_credit_tuples %}
<p>{% if name.full_name %}
<a href="{% url profile_main user_id=name.id %}">{{name.full_name}}</a>
{% else %}
{{name}}
{% endif %}
<a href="{% url videoplayer video_id=video.id %}">{{video}}</a>
({% for position in positions %}{% ifchanged %}{{position}}{% endifchanged %}{% if not forloop.last %}, {% endif %}{% endfor %})
{% endfor %}

为什么这个 View 在什么地方创建了这么多的数据库查询?如何以及以哪种方式使该 View 功能更有效/更好?谢谢你。

最佳答案

您将需要研究select_related()(https://docs.djangoproject.com/en/1.3/ref/models/querysets/#select-related)以解决您的查询泄漏问题。如果您提前知道您将要查看与外键相关的模型数据,则需要添加select_related。更好的是,如果您只知道几个外键,则可以仅添加所需的外键。

每当您看到django运行的查询数量超出您的预期时,select_related几乎总是正确的答案

关于django - 减少Django中的数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6513782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com