Django外键模型计数 [英] django count of foreign key model
本文介绍了Django外键模型计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示我的问题模型的答案
我的模型:
class Question(models.Model):
text = models.TextField()
title = models.CharField(max_length = 200)
日期= models.DateTimeField(默认= datetime.datetime.now)
作者= models.ForeignKey(CustomUser)
标签= models.ManyToManyField(Tags)
def __str __(self) :
return self.title
class答案(models.Model):
text = models.TextField()
date = models.DateTimeField(default = datetime.datetime.now)
喜欢= models.IntegerField(default = 0)
author = models.ForeignKey(CustomUser)
问题= models.ForeignKey(Question)
我的观点:
def all_questions(请求):
个问题= Question.objects.all()
个答案= Answer.objects.filter(question_id = questions).count()
r eturn render(request,'all_questions.html',{
'questions':questions,'answers':answers})
立即视图显示所有答案的计数。如何通过问题
模型进行过滤?
解决方案
您可以使用 .annotate()
获取与每个问题
相关的个答案
的数量。
django.db.models中的
import Count
个问题= Question.objects.annotate(number_of_answers = Count('answer'))#注释查询集
这样做,每个 question
对象将具有一个额外的属性 number_of_answers
个具有与每个问题
个答案的数量的值>。
问题[0]。number_of_answers#使用 number_of_answers属性
<访问与问题相关的答案数量/ code>
最终代码:
<$ p $来自django.db.models的p>
import Count
def all_questions(请求):
个问题= Question.objects.annotate(number_of_answers = Count('answer') )
return render(request,'all_questions.html',{
'questions':questions})
在您的模板中,您可以执行以下操作:
{%forquestion %%}
{{question.number_of_answers}}#显示与此问题相关的答案的数量
Hi i want to display a count of answers to my question model
my model:
class Question(models.Model):
text = models.TextField()
title = models.CharField(max_length=200)
date = models.DateTimeField(default=datetime.datetime.now)
author = models.ForeignKey(CustomUser)
tags = models.ManyToManyField(Tags)
def __str__(self):
return self.title
class Answer(models.Model):
text = models.TextField()
date = models.DateTimeField(default=datetime.datetime.now)
likes = models.IntegerField(default=0)
author = models.ForeignKey(CustomUser)
question = models.ForeignKey(Question)
my view:
def all_questions(request):
questions = Question.objects.all()
answers = Answer.objects.filter(question_id=questions).count()
return render(request, 'all_questions.html', {
'questions':questions, 'answers':answers })
Right now view displays count of all answers. How can i filter it by the Question
model?
解决方案
You can use .annotate()
to get the count of answers
associated with each question
.
from django.db.models import Count
questions = Question.objects.annotate(number_of_answers=Count('answer')) # annotate the queryset
By doing this, each question
object will have an extra attribute number_of_answers
having the value of number of answers
associated to each question
.
questions[0].number_of_answers # access the number of answers associated with a question using 'number_of_answers' attribute
Final Code:
from django.db.models import Count
def all_questions(request):
questions = Question.objects.annotate(number_of_answers=Count('answer'))
return render(request, 'all_questions.html', {
'questions':questions})
In your template, then you can do something like:
{% for question in questions %}
{{question.number_of_answers}} # displays the number of answers associated with this question
这篇关于Django外键模型计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文