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

2

14

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

Secor answered 7/11, 2017 at 19:54 Comment(0)
A
16

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)
Antipathy answered 26/6, 2018 at 5:56 Comment(1)
I am getting "tags": "catalog.Product.None". Not working!Romaic
C
11

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()

Cage answered 4/4, 2019 at 21:39 Comment(2)
By the way this should most likely be placed in the class that inherits graphene_django.DjangoObjectType .Efficacy
Another approach to avoid having to add a get_tags property ``` return graphene.List( graphene.String, description=field.help_text, required=not field.null, resolver=lambda obj, info: obj.tags.all() ) ```Tumular

© 2022 - 2024 — McMap. All rights reserved.