Django - QuerySet 中的唯一列表
我有一个过滤后的查询集,其中有一个多对多字段“客户端”。我想创建查询集中所有客户端对象的唯一字典,因此:
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)
我想最终得到客户端对象的字典:
{<Client: 1>,<Client: 2>,<Client: 3>}
一些帮助将不胜感激:)
I have a filtered QuerySet which has a ManyToMany field 'Client'. I want to create 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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
链接到 Django 文档 关于 queryset unique() 方法
Link to Django docs on queryset distinct() method