Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django';s ListView-如何定制它_Python_Django_Django Models_Django Views_Django Generic Views - Fatal编程技术网

Python Django';s ListView-如何定制它

Python Django';s ListView-如何定制它,python,django,django-models,django-views,django-generic-views,Python,Django,Django Models,Django Views,Django Generic Views,如果这是重复的话,我很抱歉,但是我没有找到我的问题的任何答案 我的问题如下 我真的很难理解在使用ListView时如何定制我的视图。我阅读了Django文档,但我发现它非常简短 我想做的是计算每个主题的问题,并返回问题最多的主题。在我的models.py(主题类)中,我可以使用以下代码行计算每个主题的问题数: 然而,我只想返回最热门的话题和问题的数量 使用基于函数的视图,我将循环主题,并创建两个变量来存储主题名称和问题数量。但是,对于ListView,我不知道是使用get_context_dat

如果这是重复的话,我很抱歉,但是我没有找到我的问题的任何答案

我的问题如下

我真的很难理解在使用ListView时如何定制我的视图。我阅读了Django文档,但我发现它非常简短

我想做的是计算每个主题的问题,并返回问题最多的主题。在我的models.py(主题类)中,我可以使用以下代码行计算每个主题的问题数:

然而,我只想返回最热门的话题和问题的数量

使用基于函数的视图,我将循环主题,并创建两个变量来存储主题名称和问题数量。但是,对于ListView,我不知道是使用get_context_data()、get_queryset()还是其他什么

我的视图类

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)    
    ....
{% for t in topics|dictsortreversed:"count_of_questions" %}
    {{ t.count_of_questions }}
{% endfor %}
我的型号

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)    
    ....
{% for t in topics|dictsortreversed:"count_of_questions" %}
    {{ t.count_of_questions }}
{% endfor %}

您可能希望尝试使用
.annotate()
获取与每个
主题相关的
问题的计数:

from django.db.models import Count

topics_count = Topics.objects.annotate(count_of_questions=Count('question'))
每个
主题
对象将有一个新属性
count\u of \u questions
,它是与每个
主题
相关联的问题总数

列表视图中

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'))
然后,您应该能够使用在模板中执行以下操作,该模板应首先返回问题最多的主题

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)    
    ....
{% 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

有更有效的解决方案吗?

谢谢您的提问。但是,我希望只返回问题最多的Topic实例。我不想返回查询集。你的评论让我有点困惑——如果你想使用Django的
ListView
,这是最有效的方法。在您的解决方案中,您将添加到
get\u context\u data()
,而我将添加到
get\u queryset()
——这两者没有真正的区别。要仅显示问题最多的
主题
,只需将模板中的结果限制在第一条记录?如果你解释一下你试图通过获取变量中的问题数(比如if/else语句或其他什么)来完成什么,可能会有所帮助?