Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't know how to convert the Django field skills (<class 'taggit.managers.TaggableManager'>)? graphql

I am trying to change my rest end points to graphql and I had a library called TaggableManager as one of the model fields. Anyone know how this can work with graphql?

Thanks in advance

like image 434
Tsuna Avatar asked Nov 07 '17 19:11

Tsuna


2 Answers

You have to explicitly tell graphene how to convert the TaggableManger field to be used in Queries. Register @convert_django_field from graphene_django.converter before using your model with the TaggableManger field either in Queries or Mutations.

Example:

from graphene_django.converter import convert_django_field
from taggit.managers import TaggableManager

# convert TaggableManager to string representation
@convert_django_field.register(TaggableManager)
def convert_field_to_string(field, registry=None):
    return String(description=field.help_text, required=not field.null)
like image 121
Deepak Sood Avatar answered Nov 11 '22 23:11

Deepak Sood


I made one version of the Deepak Sood answer what works for me

from graphene import String, List
from taggit.managers import TaggableManager
from graphene_django.converter import convert_django_field

@convert_django_field.register(TaggableManager)
def convert_field_to_string(field, registry=None):
    return List(String, source='get_tags')

And in the tagger model, i create a property names get_tags:

    .
    .
    .
    tags = TaggableManager()

    @property
    def get_tags(self):
        return self.tags.all()

like image 23
Fernando Espinoza Avatar answered Nov 11 '22 23:11

Fernando Espinoza