graphene-django: AttributeError: 'NoneType' object has no attribute '_meta'

I have simple graphene based django project: https://github.com/aliev/graphene-django-bug

For some reason when I’m trying to run server or make migrations I have an exception:

AttributeError: 'NoneType' object has no attribute '_meta'

I’m using model forms and DjangoModelFormMutation

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 17 (2 by maintainers)

Most upvoted comments

I’m having the same issue with the rest framework SerializerMutation

I’ve narrowed it down to that I need to import only those types that have a relation (ForeignKey or OneToOne) to another type. Would you like me to provide a simple example to reproduce the issue, would that help?

@phalt this bug is still current AFAIK. Not sure why this has been closed? To confirm, when putting mutations into a Python file separate from the type definitions I need to import all the types related to the mutations at the top of the file. Even though I don’t use them. Otherwise AttributeError: 'NoneType' object has no attribute '_meta' is raised when running unit tests.

  File "/django-project/api/schema.py", line 42, in <module>
    schema = graphene.Schema(query=Query, mutation=Mutation)
  File "/usr/local/lib/python3.6/site-packages/graphene/types/schema.py", line 62, in __init__
    self.build_typemap()
  File "/usr/local/lib/python3.6/site-packages/graphene/types/schema.py", line 126, in build_typemap
    initial_types, auto_camelcase=self.auto_camelcase, schema=self
  File "/usr/local/lib/python3.6/site-packages/graphene/types/typemap.py", line 80, in __init__
    super(TypeMap, self).__init__(types)
  File "/usr/local/lib/python3.6/site-packages/graphql/type/typemap.py", line 28, in __init__
    self.update(reduce(self.reducer, types, OrderedDict()))  # type: ignore
  File "/usr/local/lib/python3.6/site-packages/graphene/types/typemap.py", line 88, in reducer
    return self.graphene_reducer(map, type)
  File "/usr/local/lib/python3.6/site-packages/graphene/types/typemap.py", line 117, in graphene_reducer
    return GraphQLTypeMap.reducer(map, internal_type)
  File "/usr/local/lib/python3.6/site-packages/graphql/type/typemap.py", line 106, in reducer
    field_map = type.fields
  File "/usr/local/lib/python3.6/site-packages/graphql/pyutils/cached_property.py", line 22, in __get__
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/usr/local/lib/python3.6/site-packages/graphql/type/definition.py", line 221, in fields
    return define_field_map(self, self._fields)
  File "/usr/local/lib/python3.6/site-packages/graphql/type/definition.py", line 235, in define_field_map
    field_map = field_map()
  File "/usr/local/lib/python3.6/site-packages/graphene/types/typemap.py", line 274, in construct_fields_for_type
    map = self.reducer(map, field.type)
  File "/usr/local/lib/python3.6/site-packages/graphene/types/typemap.py", line 88, in reducer
    return self.graphene_reducer(map, type)
  File "/usr/local/lib/python3.6/site-packages/graphene/types/typemap.py", line 117, in graphene_reducer
    return GraphQLTypeMap.reducer(map, internal_type)
  File "/usr/local/lib/python3.6/site-packages/graphql/type/typemap.py", line 106, in reducer
    field_map = type.fields
  File "/usr/local/lib/python3.6/site-packages/graphql/pyutils/cached_property.py", line 22, in __get__
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/usr/local/lib/python3.6/site-packages/graphql/type/definition.py", line 221, in fields
    return define_field_map(self, self._fields)
  File "/usr/local/lib/python3.6/site-packages/graphql/type/definition.py", line 235, in define_field_map
    field_map = field_map()
  File "/usr/local/lib/python3.6/site-packages/graphene/types/typemap.py", line 275, in construct_fields_for_type
    field_type = self.get_field_type(map, field.type)
  File "/usr/local/lib/python3.6/site-packages/graphene/types/typemap.py", line 333, in get_field_type
    return GraphQLList(self.get_field_type(map, type.of_type))
  File "/usr/local/lib/python3.6/site-packages/graphene/types/typemap.py", line 336, in get_field_type
    return map.get(type._meta.name)
AttributeError: 'NoneType' object has no attribute '_meta'

In my root schema.py file, I imported the mutation before the query which resulted in an error. Putting query import before mutation imports resolved the issue for me

Same problem here, it happens when I define types in separate files.

In my case the error originated from defining the SerializerMutation class before the Type class which I needed in the SerializerMutation. After moving all schema types above the mutations it worked.