Search code examples
pythondjangographene-python

Graphene Django image field get absolute path


I have an image field in my Django model and I am trying to get absolute path of the image field output from Graphene. I remember getting the absolute uri of a file/image field using HttpRequest.build_absolute_uri. So, I decided to use the same function in Graphene Django:

class PersonType(DjangoObjectType):
    def resolve_photo(self, info, **kwargs):
        print(info.context) # WSGIRequest
        print(info.context.build_absolute_uri(self.photo)) # Error here
        return self.photo

    class Meta:
       model = Person

Because the request here is not Django's HttpRequest (it is WSGI Request), I am not able to use some utility functions of Django's request.

Is there a way to create HttpRequest from WSGIRequest or is there some other way of building full URLs in Graphene Django? Reading docs, source code, or resources in the internet, I am not able to find a solution to my problem.


Solution

  • Is there a way to create HttpRequest from WSGIRequest..?

    WSGIRequest inherits from django.http.HttpRequest so, you should be able to use all "public" utility functions of Django's request.

    So having there a WSGIRequest is not your real problem. If info.context is really a WSGIRequest, you should can:

     info.context.build_absolute_uri(self.photo)
    

    without any problems.

    My guess

    I've to guess here since you don't post the error you're receiving, that the problem is in self.photo.