2

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.

pppery
  • 3,731
  • 22
  • 33
  • 46
Udemezue John
  • 128
  • 2
  • 12

2 Answers2

2

If Profile.avatar is ImageField you should use self.avatar.url instead of self.avatar:

info.context.build_absolute_uri(self.avatar.url)
ar7n
  • 171
  • 1
  • 6
0

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)

Udemezue John
  • 128
  • 2
  • 12