简体   繁体   English

django 如何获取 manytomany 字段的计数

[英]django how to get count for manytomany field

I have model for question:我有 model 问题:

class Question(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=120)
    description = models.TextField()
    answers = models.ManyToManyField('Answer',related_name='answer_name', blank=True)
    post_date = models.DateTimeField(auto_now=True)

def __unicode__(self):
    return self.title

And I have model for answer:我有 model 回答:

class Answer(models.Model):
    user = models.ForeignKey(User)
    question = models.ForeignKey(Question)
    ans_body = models.TextField()
    post_date = models.DateTimeField(auto_now=True)

def __unicode__(self):
    return self.ans_body

Question creation and answer submission are working perfectly.问题创建和答案提交工作完美。 I cant correctly show answer for particular question.我无法正确显示特定问题的答案。 But when I try to get the count of answer for particular question its not showing.但是,当我尝试获取特定问题的答案数时,它没有显示。 It displays 0 count.它显示 0 计数。

In my view I am getting the list of the answer by:在我看来,我通过以下方式获得答案列表:

context["question_list"] = Question.objects.all()

And in my template在我的模板中

{% for question in question_list %}
   {{ question.title }}
    Ans:{{question.answers.count}}
{% endfor %}

When I do this I get the count 0 if there are answers.当我这样做时,如果有答案,我会得到计数 0。 How can I get the count of the answers for particular questions.我怎样才能得到特定问题的答案数。

This worked: 这有效:

{{question.answer_set.count}}

Happy.. 快乐..

You can do something like {{ question.answers.all.count }} , but if you are iterating over more than question it will cause a database query for every question. 您可以执行类似{{ question.answers.all.count }} ,但如果您要迭代而不是问题,则会导致对每个问题进行数据库查询。

If you want to annotate the whole queryset with the count for each question: 如果要使用每个问题的计数来注释整个查询集:

from django.db.models import Count

context['question_list'] = Question.objects.all().annotate(
    answer_count=Count('answers')
)

Then you can access the count for each question with {{ question.answer_count }} . 然后,您可以使用{{ question.answer_count }}访问每个问题的计数。

Why not use: Question.objects.all().count() 为什么不使用: Question.objects.all().count()

For my project, I have a Field in 'Info' Model 对于我的项目,我在“信息”模型中有一个字段

users_like = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="%(app_label)s_%(class)s_likes", blank=True)

I use below code to count the number of like then show it in Admin List Page. 我使用下面的代码计算喜欢的数量然后在管理员列表页面中显示它。

# admin.py
from django.contrib import admin

from .models import Info
class InfoAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'like_count',)
    def like_count(obj):
        return obj.users_like.all().count()

admin.site.register(Info, InfoAdmin)

The result is: 结果是: 图片 Hope these can help you! 希望这些可以帮到你!

This will work for you without any problem in your case:在您的情况下,这对您没有任何问题:

{{question.answer_name.count}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM