Get list of Unique many-to-many records from a queryset
Posted
by rsp
on Stack Overflow
See other posts from Stack Overflow
or by rsp
Published on 2010-06-09T03:20:08Z
Indexed on
2010/06/09
3:22 UTC
Read the original article
Hit count: 405
My models:
class Order(models.Model):
ordered_by = models.ForeignKey(User)
reasons = models.ManyToManyField(Reason)
class Reason(models.Model):
description = models.CharField()
Basically a user creates an order and gives reasons for that order. ie, john has two orders (one for pencils because he is out AND because he doesn't like his current stock. a second order for pens because he is out).
I want to print a list out of all reasons a user has placed his orders. So under john, it should print "he is out", "he doesn't like his current stock"; those two lines only. If I simply select all of john's orders, iterate through them and print out their "reasons" it'll print "he is out", "he doesn't like his current stock" and then "he is out" again. I don't want these duplicate values.
How do I select a list of his reasons for ALL his orders so that the list has all unique rows?
© Stack Overflow or respective owner