Search code examples
djangographene-python

How to access Meta on CustomNode using Graphene and Relay


I want to make a generic get_node_from_global_id, so I need to get the root model the query is requesting and then return a row from that table. To do this I want to use model = getattr(Query,info.field_name).field_type.Meta.model .

The first part, getattr(Query,info.field_name).field_type gets me TableNameNode from Query using info.field_name. But when I try to access ...Meta.model I get an error saying that there is no attribute Meta on TableNameNode. I can see that there is a nested class Meta so how can I access it?

from graphene_django import DjangoObjectType
from graphene import relay

class CustomNode(relay.Node): 
    class Meta:
        name = 'Node'

    @staticmethod
    def to_global_id(type, id):
        #returns a non-encoded ID
        return id

    @staticmethod
    def get_node_from_global_id(info, global_id, only_type=None):
        user = info.context.user
        model = getattr(Query,info.field_name).field_type.Meta.model
        #return row here...
        pass


class Query(object):
    tablename = CustomNode.Field(TableNameNode)

class TableNameNode(DjangoObjectType):
    class Meta:
        model = TableName
        interfaces = (CustomNode,)

Solution

  • After digging around here I got the idea that I should try ._meta to access the Meta nested class. I don't know the rationale behind this, but when I changed my code to model = getattr(Query,info.field_name).field_type._meta.model it worked.