如何在 Django 1.9 中连接多个表?
How can I join multiple tables in Django 1.9?
我需要协助。我的问题是我试图在 Django 1.9 中加入多个 tables/models ((Question, Answer, User)) 以获得有多少答案有一组问题由某个用户。
我已经sql查询了我想要的内容:
SELECT q.id, q.title, COUNT(a.id) AS total_answers
FROM review_question AS q
JOIN review_answer AS a ON q.id = a.question_id
JOIN users_user AS u ON q.user_id = u.id
WHERE q.user_id = 1
GROUP BY q.id, q.title;
这是我的模型:
review/models.py
[问题]
class Question(models.Model):
user = models.ForeignKey(User, db_index=True, null=True, blank=True)
tag = models.ManyToManyField(Tag)
title = models.CharField(max_length=200)
[回答]
class Answer(models.Model):
question = models.ForeignKey(Question)
user = models.ForeignKey(User, db_index=True, null=True, blank=True)
users/models.py
[用户]
class User(models.Model):
username = models.CharField(max_length=100, unique=True)
顺便说一下,在我的文件中 users/views.py 我有下一个:
class UserDetailView(DetailView):
model = User
def get_context_data(self, **kwargs):
# calling the get_context_data parent here
questions = Question.objects.filter(user = context['object']).order_by('created')
但是,我只得到了用户提出的所有问题。
我已经尝试了一段时间,寻找一种方法将上面的查询转换为 django-orm,但我仍然无法完成。任何帮助将不胜感激。
终于可以解决我的问题了。
接下来我做的是:
在我的文件中users/views.py
class UserDetailView(DetailView):
model = User
def get_context_data(self, **kwargs):
# Calling the get_context_data parent
questions = Question.objects.filter(user = context['object']).order_by('created')
tags = [ question.tag.all() for question in questions ]
total_answers = self.get_total(questions) # Calling the function that return the total answer by question of a specific user
context['question_tags'] = zip(questions, tags, total_answers) #Zipping the lists with results to use it in the template
return context
def get_total(self, questions):
#Return the total answers that a question has (Here is the trick!)
return [
Answer.objects.filter(question__id=question.id).annotate(num_answers=Count('question')).count() for question in questions]
这就是我所做的一切。
最后,我要特别感谢@PauloAlmeida 和@MikeVelazco 的帮助!
我需要协助。我的问题是我试图在 Django 1.9 中加入多个 tables/models ((Question, Answer, User)) 以获得有多少答案有一组问题由某个用户。
我已经sql查询了我想要的内容:
SELECT q.id, q.title, COUNT(a.id) AS total_answers
FROM review_question AS q
JOIN review_answer AS a ON q.id = a.question_id
JOIN users_user AS u ON q.user_id = u.id
WHERE q.user_id = 1
GROUP BY q.id, q.title;
这是我的模型:
review/models.py
[问题]
class Question(models.Model):
user = models.ForeignKey(User, db_index=True, null=True, blank=True)
tag = models.ManyToManyField(Tag)
title = models.CharField(max_length=200)
[回答]
class Answer(models.Model):
question = models.ForeignKey(Question)
user = models.ForeignKey(User, db_index=True, null=True, blank=True)
users/models.py
[用户]
class User(models.Model):
username = models.CharField(max_length=100, unique=True)
顺便说一下,在我的文件中 users/views.py 我有下一个:
class UserDetailView(DetailView):
model = User
def get_context_data(self, **kwargs):
# calling the get_context_data parent here
questions = Question.objects.filter(user = context['object']).order_by('created')
但是,我只得到了用户提出的所有问题。
我已经尝试了一段时间,寻找一种方法将上面的查询转换为 django-orm,但我仍然无法完成。任何帮助将不胜感激。
终于可以解决我的问题了。
接下来我做的是:
在我的文件中users/views.py
class UserDetailView(DetailView):
model = User
def get_context_data(self, **kwargs):
# Calling the get_context_data parent
questions = Question.objects.filter(user = context['object']).order_by('created')
tags = [ question.tag.all() for question in questions ]
total_answers = self.get_total(questions) # Calling the function that return the total answer by question of a specific user
context['question_tags'] = zip(questions, tags, total_answers) #Zipping the lists with results to use it in the template
return context
def get_total(self, questions):
#Return the total answers that a question has (Here is the trick!)
return [
Answer.objects.filter(question__id=question.id).annotate(num_answers=Count('question')).count() for question in questions]
这就是我所做的一切。 最后,我要特别感谢@PauloAlmeida 和@MikeVelazco 的帮助!