question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DjangoObjectType attribute of type FileField should return its url

See original GitHub issue

Current behavior is to return file name for FileField and ImageField type attributes of graphene_django.types.DjangoObjectType.

It seems completely unusable as we always need an actual url of the file or image on a client.

Thus the one is forced to implement something like the following for every FileField:

class CompanyNode(types.DjangoObjectType):
    logo = graphene.String()

    class Meta:
        model = models.Company
        interfaces = (relay.Node,)

    def resolve_logo(self, args, context, info):
        return self.logo and self.logo.url

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:7
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

25reactions
spockNinjacommented, Sep 9, 2017

It looks like FileField is being registered to the graphene String type. That means the value coming out of it is whatever six.text_type(value) will spit out when called with the file field instance. And that looks to be eventually coming from the django.core.files.base.File class.

So all that being said… I think ideally, we’d get something where you could pull whichever attributes off the file that you would need. Something like:

{
  model {
    fileField {
      url
      name
    }
}

I’m not too sure on the implementation details, but I’m thinking we would just need to implement a new class FileType(graphene.ObjectType) that would then be used to map the FileField to a graphene.Field of the new type instead of graphene.String.

How does that sound?

6reactions
jkimbocommented, Apr 1, 2020

If someone would like to contribute a PR to add this feature I will happily review and merge it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Queries & ObjectTypes - Graphene-Python
By default, DjangoObjectType will present all fields on a Model through GraphQL. If you only want a subset of fields to be present,...
Read more >
how to add a custom field that is not present in database in ...
Graphene uses Types to resolve nodes, which are not at all tied to the model, you can even define a Graphene Type which...
Read more >
Django model data types and fields list - GeeksforGeeks
Fields in Django are the data types to store a particular type of ... ImageField, It inherits all attributes and methods from FileField, ......
Read more >
Create a Modern Application with Django and Vue – Part Three
First, we need to setup a URL pattern to serve the GraphQL APIs. ... from blog import models # Define type class SiteType(DjangoObjectType): ......
Read more >
Create a Modern Application with Django and Vue #2
First, we need to setup a URL pattern to serve the GraphQL APIs. ... models # Define type class SiteType(DjangoObjectType): class Meta: model...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found