Reputation: 23
I have the following model in Django:
class Feedback(models.Model):
solution=models.ForeignKey(Solution, related_name='solution1')
idea=models.ForeignKey(Idea,null=True, blank=True,related_name='idea1')
idea2=models.ForeignKey(Idea, related_name='idea2')
Given the ID of a solution how can I write a query that would retrieve me all ideas2 that have been in the feedbacks with this solution. And I am wondering is it even possible in Django. Thanks a lot! I tried something like this, but how can I specify that I am looking for idea2, not idea1?
ideas2= Idea.objects.filter(feedback_solution1=solutionID)
Upvotes: 0
Views: 714
Reputation: 3800
feedbacks = list(Feedback.objects.filter(solution=solutionID).values_list('idea2__pk', flat=True))
ideas2 = Idea.objects.filter(pk__in=feedbacks)
Or
feedbacks = list(Feedback.objects.filter(solution=solutionID).values_list('idea2__pk', flat=True))
ideas2 = Idea.objects.in_bulk(feedbacks)
Check out the Performance considerations section in the Documentation.
Upvotes: 2