开发者

Django - Unique list from QuerySet

开发者 https://www.devze.com 2023-02-28 02:23 出处:网络
I have a filtered QuerySet which has a ManyToMany field \'Client\'. I want to cr开发者_如何学JAVAeate a unique dict of all the Client objects in the query set so:

I have a filtered QuerySet which has a ManyToMany field 'Client'. I want to cr开发者_如何学JAVAeate a unique dict of all the Client objects in the query set so:

Projects Queryset:
- Project1.client = <Client: 1>
- Project2.client = <Client: 1>
- Project3.client = <Client: 2>
- Project4.client = <Client: 2>
- Project5.client = <Client: 3>

class Project(models.Model):
    client = models.ForeignKey(Client, blank=True, null=True)

I want to end up with a dict of client objects:

{<Client: 1>,<Client: 2>,<Client: 3>}

Some help would be appreciated :)


Project.objects.values('client').distinct()

Link to Django docs on queryset distinct() method

0

精彩评论

暂无评论...
验证码 换一张
取 消