Django 的 ListView - 如何自定义它
Django's ListView - How to customise it
如果这是重复的,我很抱歉,但我没有找到我的问题的任何答案。
下面是我的问题
我真的很难理解如何在使用 ListView 时自定义我的视图。我阅读了 Django 文档,但我发现它非常简短。
我想做的是计算每个主题的问题和return问题最多的主题.我可以在 models.py (Topic class):
中使用以下代码行计算每个主题的问题数量
def questions_count(self):
return Question.objects.filter(question__topic = self).count()
不过,我想return只选择最热门的Topic和题目数。
使用基于函数的视图,我将遍历主题,并创建两个变量来存储主题名称和问题数量。但是,对于 ListView,我不知道是否使用 get_context_data()、get_queryset() 或其他东西。
我的观点class:
class TopicsView(ListView):
model = Topic
context_object_name = 'topics'
template_name = 'home.html'
我的模特:
class Topic(models.Model):
name = models.CharField(max_length=30, unique=True)
description = models.CharField(max_length=100)
class Question(models.Model):
....
topic = models.ForeignKey(Topic, related_name='questions', on_delete=models.CASCADE)
....
您可能想尝试使用 .annotate()
来获取与每个 Topic
:
关联的 Questions
的计数
from django.db.models import Count
topics_count = Topics.objects.annotate(count_of_questions=Count('question'))
每个 Topic
对象都有一个新属性 count_of_questions
,即与每个 Topic
.
关联的问题总数
在你的ListView
中:
class TopicsView(ListView):
model = Topic
context_object_name = 'topics'
template_name = 'home.html'
def get_queryset(self):
queryset = super(TopicsView, self).get_queryset()
queryset = queryset.annotate(count_of_questions=Count('question'))
那么您应该可以使用 dictsortreversed 在您的模板中执行以下操作,应该 return 问题最多的主题 第一个:
{% for t in topics|dictsortreversed:"count_of_questions" %}
{{ t.count_of_questions }}
{% endfor %}
下面有类似的问答
最终,我通过get_context_data()函数成功完成了。这就是我的解决方案:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
mostQuestions = context['object_list'][0].questions_count()
mostQuestionsTopic = context['object_list'][0]
for topic in context['object_list']:
if topic.questions_count() > mostQuestions:
mostQuestions = topic.questions_count()
mostQuestionsTopic = topic
context['mostQuestions'] = mostQuestions
context['mostQuestionsTopic'] = mostQuestionsTopic
return context
有没有更高效的解决方案?
如果这是重复的,我很抱歉,但我没有找到我的问题的任何答案。
下面是我的问题
我真的很难理解如何在使用 ListView 时自定义我的视图。我阅读了 Django 文档,但我发现它非常简短。
我想做的是计算每个主题的问题和return问题最多的主题.我可以在 models.py (Topic class):
中使用以下代码行计算每个主题的问题数量def questions_count(self):
return Question.objects.filter(question__topic = self).count()
不过,我想return只选择最热门的Topic和题目数。
使用基于函数的视图,我将遍历主题,并创建两个变量来存储主题名称和问题数量。但是,对于 ListView,我不知道是否使用 get_context_data()、get_queryset() 或其他东西。
我的观点class:
class TopicsView(ListView):
model = Topic
context_object_name = 'topics'
template_name = 'home.html'
我的模特:
class Topic(models.Model):
name = models.CharField(max_length=30, unique=True)
description = models.CharField(max_length=100)
class Question(models.Model):
....
topic = models.ForeignKey(Topic, related_name='questions', on_delete=models.CASCADE)
....
您可能想尝试使用 .annotate()
来获取与每个 Topic
:
Questions
的计数
from django.db.models import Count
topics_count = Topics.objects.annotate(count_of_questions=Count('question'))
每个 Topic
对象都有一个新属性 count_of_questions
,即与每个 Topic
.
在你的ListView
中:
class TopicsView(ListView):
model = Topic
context_object_name = 'topics'
template_name = 'home.html'
def get_queryset(self):
queryset = super(TopicsView, self).get_queryset()
queryset = queryset.annotate(count_of_questions=Count('question'))
那么您应该可以使用 dictsortreversed 在您的模板中执行以下操作,应该 return 问题最多的主题 第一个:
{% for t in topics|dictsortreversed:"count_of_questions" %}
{{ t.count_of_questions }}
{% endfor %}
下面有类似的问答
最终,我通过get_context_data()函数成功完成了。这就是我的解决方案:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
mostQuestions = context['object_list'][0].questions_count()
mostQuestionsTopic = context['object_list'][0]
for topic in context['object_list']:
if topic.questions_count() > mostQuestions:
mostQuestions = topic.questions_count()
mostQuestionsTopic = topic
context['mostQuestions'] = mostQuestions
context['mostQuestionsTopic'] = mostQuestionsTopic
return context
有没有更高效的解决方案?