Reputation: 1254
I have a query like this:
last_orders = PsSiparis.objects.filter(kullanici = request.user).order_by("-id")[:4]
for order in last_orders:
print order.restoran
And it outputs:
Bafra Pide
Bafra Pide
Deneme Restoran
Bafra Pide
I want to use distinct() attribute so my list will look like this:
Bafra Pide
Deneme Restoran
But I couldn't find the way. Any help will be appreciated.
Upvotes: 0
Views: 4399
Reputation: 174614
You should use a set
, which will remove duplicates:
>>> l = ['Bafra Pide','Bafra Pide','Deneme Restoran','Bafra Pide']
>>> s = set(l)
>>> for i in s:
... print i
...
Bafra Pide
Deneme Restoran
You should add the individual names to the list and not the actual objects.
Upvotes: 3
Reputation: 9102
Django comes with a method for selecting non equal results of a query. Give it a look: Django documentation, distinct()
edition: Also, limiting a Queryset returns a Queryset, so you are not gonna have problem with it, I would recommend slicing after applying distinc() method. Limiting a Queryset
Upvotes: 0
Reputation: 798516
You can't call .distinct()
on a sliced query since there's no simple way to generate the appropriate SQL query. You will need to postprocess using set
or the like.
Upvotes: 1