Django graphene中继将查询限制为用户拥有的对象

Django graphene中继将查询限制为用户拥有的对象,django,graphene-python,Django,Graphene Python,我正在做关于使用继电器过滤的石墨烯教程,来自: 其中,用户仅限于查询他们以前创建的对象。我使用的是Graphene2、Django2和DjangoFilter1.11 class AnimalFilter(django_filters.FilterSet): # Do case-insensitive lookups on 'name' name = django_filters.CharFilter(lookup_expr=['iexact']) #changed this t

我正在做关于使用继电器过滤的石墨烯教程,来自: 其中,用户仅限于查询他们以前创建的对象。我使用的是Graphene2、Django2和DjangoFilter1.11

class AnimalFilter(django_filters.FilterSet):
    # Do case-insensitive lookups on 'name'
    name = django_filters.CharFilter(lookup_expr=['iexact']) #changed this to work

    class Meta:
        model = Animal
        fields = ['name', 'genus', 'is_domesticated']

    @property
    def qs(self):
        # The query context can be found in self.request.
        return super(AnimalFilter, self).qs.filter(owner=self.request.user)
我正在插入用户数据加载的
self.request.user
部分。当我执行以下查询时:

query {
  allAnimalss {
    edges {
      node {
        id,
        name
      }
    }
  }
}
我在查询字段中得到一个错误:

{
  "errors": [
    {
      "message": "'NoneType' object has no attribute 'user'",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ]
    }
  ],
  "data": {
    "allAnimals": null
  }
}
如果我拆下过滤器,它工作正常。教程提到“由经过身份验证的用户拥有(在context.user中设置)。”这是什么意思

我尝试向
views.py

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['user'] = self.request.user
    return context

还可以将
self.request.user
更改为
self.context.user
,但它不起作用

您可以通过解析器方法中的info.context访问请求,从而访问用户。医生们对此解释得并不好,但是


在query类下使用resolve_something的示例是有效的。但“过滤基于ID的节点访问”中的示例并不适用
def resolve_something(self, info, something_id):
    # Here info.context is the django request object
    something = Something.objects.get(something_id)
    if info.context.user.id == something.user_id:
        # The user owns this object!
        return something

    # Return None or raise an exception here maybe since it's not the owner
    return None