smorele
smorele

Reputation: 157

Invalid distinct with django

I try to get a cars list on my Django project but i'm in trouble with ORM

class Car(models.Model):
    name = models.CharField(max_length=200)
    owner = models.ForeignKey(User)

With Car.objects.all() I have a list as:

- car#1, user#1
- car#1, user#2
- car#1, user#3
- car#2, user#4
- car#3, user#4

what I would like is:

- car#1
- car#2
- car#3

Then, all cars are distinct by the name, regardless of owner

I've try something like

Cars.objects.all().annotate(Count('owner', distinct=True))

but I still have all cars. Can someone help me with this point ? Documentation suggests annotate and aggregate but it's still hars to understand it.

Upvotes: 0

Views: 26

Answers (1)

Exprator
Exprator

Reputation: 27523

try this

 Cars.objects.values('name').distinct()

Upvotes: 1

Related Questions