【问题标题】:Django creating queryset filtered by group member ship results in errorDjango按组成员身份创建查询集过滤器会导致错误
【发布时间】:2014-10-24 12:48:00
【问题描述】:

我需要创建一个查询集。该类是分配,并具有 2 个属性(以及其他一些属性)用户和组。用户和组是默认的 django auth 用户和组。所以用户与组有多对多的关系。

class Allocation(models.Model):
    user = models.ForeignKey(User)
    group = models.ForeignKey(Group)

查询集应该只包含用户所属组的对象。下面的尝试导致

TypeError at /allocation/
'SQLEvaluator' object is not iterable

尝试:

queryset=Allocation.objects.select_related().filter(user__groups__contains=F('group'))

【问题讨论】:

    标签: django django-queryset


    【解决方案1】:

    您不需要__contains。

    Allocation.objects.select_related().filter(user__groups=F('group'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 2014-03-10
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多