Django graphene relay restricting queries to objects owned the user

扶醉桌前 提交于 2019-12-25 00:27:24

问题


I am doing the graphene tutorial on filtering with relay from: http://docs.graphene-python.org/projects/django/en/latest/filtering/ where the user is restricted to query objects that they previously created. I am using graphene 2, django 2, and django-filter 1.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)

I am having an inserting the self.request.user part, where the user data is loaded. When I do a query like:

query {
  allAnimalss {
    edges {
      node {
        id,
        name
      }
    }
  }
}

I get an error in the query field:

{
  "errors": [
    {
      "message": "'NoneType' object has no attribute 'user'",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ]
    }
  ],
  "data": {
    "allAnimals": null
  }
}

If I remove the filter it works fine. The tutorial mentioned "owned by the authenticated user (set in context.user)." what does this mean?

I tried adding a get_context_data function to the views.py

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

and also changing self.request.user to self.context.user but it does not work


回答1:


You can access the request and thereby the user via info.context in the resolver method. The docs are not great explaining this, but here you can see an example

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


来源:https://stackoverflow.com/questions/50916106/django-graphene-relay-restricting-queries-to-objects-owned-the-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!