Search code examples
djangographene-django

Graphene Django File 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 so as to connect to my client.

class ProfileType(DjangoObjectType): 
     class Meta: model = Profile

     def resolve_avatar(self, info, **kwargs):

    print(info.context.build_absolute_uri(self.avatar))

I do not get an absolute file field path.


Solution

  • So I got an answer to this and I want to make sure I post it someone else does not make the same mistake.

    Here is what the code should look like

    class ProfileType(DjangoObjectType):
    
        class Meta:
    
            model = Profile
    
        avatar          = graphene.String()
        cover_photo     = graphene.String()
    
    
        def resolve_avatar(self, info):
    
            return info.context.build_absolute_uri(self.avatar.url)
    
        def resolve_cover_photo(self, info):
    
            return info.context.build_absolute_uri(self.cover_photo.url)