Select distinct values from a table field

前端 未结 3 1413
小鲜肉
小鲜肉 2020-12-12 12:44

I\'m struggling getting my head around the Django\'s ORM. What I want to do is get a list of distinct values within a field on my table .... the equivalent of one of the fol

3条回答
  •  自闭症患者
    2020-12-12 13:20

    By example:

    # select distinct code from Platform where id in ( select platform__id from Build where product=p)
    pl_ids = Build.objects.values('platform__id').filter(product=p)
    platforms = Platform.objects.values_list('code', flat=True).filter(id__in=pl_ids).distinct('code')
    platforms = list(platforms) if platforms else []
    

提交回复
热议问题