我有以下型号,什么我想是基于通过review_question_id组选择的企业的集合,他们每个人的AVG(率)的集合。
这里有必要的机型:
class ReviewQuestion(models.Model):
"""Represents a question to be given in a business type
"""
business_type = models.ForeignKey(BusinessType, on_delete=models.CASCADE)
question_text = models.CharField(max_length=100)
class Business(models.Model):
"""Values for a specific business, based on a type
will inherit questions and reviews
"""
business_type = models.ForeignKey(BusinessType, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
class CustomerReview(models.Model):
"""Rate value by customers on a specific quesiton
for a specific business
"""
business = models.ForeignKey(Business, on_delete=models.CASCADE)
review_question = models.ForeignKey(
ReviewQuestion, on_delete=models.CASCADE)
review_value = models.PositiveSmallIntegerField()
最近查询我试图去接近这个:
items = Business.objects.filter(business_type_id=type_id).values(
'id', 'name', 'business_type_id', 'address', 'customerreview__review_question_id').annotate(rate=Avg('customerreview__review_value'))
与它的问题是重复。 它复制整个列表,结果是平的,那种当你在平板TSQL写它,你会得到什么。
理想的结果看起来是这样的:
[
{
"business_id": 1,
"business_name": "something",
"rating":[
{
"Question_1":{
"title":"ReviewQuestion__question_text",
"Avg":5.0
},
"Question_2":{
"title":"ReviewQuestion__question_text",
"Avg":5.0
},{
...
}
}
]
}
]
因为我是新来的Python / Django的任何帮助,将不胜感激。