【发布时间】:2011-09-24 17:18:16
【问题描述】:
我有一个视图,它可以搜索电影学分数据库,然后像这样转换和返回结果 --
# 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')
...
类位置(models.Model): 位置 = models.CharField(max_length=100) ordering = 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 %}
这个视图为什么以及在哪里创建了这么多数据库查询?我如何以及以何种方式使这个视图功能更有效/更好?谢谢你。
【问题讨论】:
-
我发现 list_of_keys = [(name, video) for name, video, locations in list_of_credit_tuples] 存在问题。你为什么要获得位置然后再使用它?您还试图迭代一个列表,除非您遗漏了某些内容,否则此时该列表为空
-
@John 我只检查
(name, video)而不是(name, video, position)的原因是因为我想为该名称-视频对创建所有职位的列表。这是否回答了您所指的内容,还是有所不同? -
正如我在下面所说,您的实际问题的答案与选择相关:list_of_credit_tuples = []。下一次 list_of_credit_tuples 被引用是在列表理解中。这意味着在你的 if 语句的那个分支中,它总是一个空列表。除非您以某种方式设置它,否则您会忽略
标签: django django-models django-templates django-views