Graphene-django: AttributeError: 'ForeignKey' object has no attribute 'rel'

Created on 22 Feb 2018  路  3Comments  路  Source: graphql-python/graphene-django

As I was following the docs for Graphene and Django Tutorial using Relay I ran into this error (relevant part):

Traceback (most recent call last):
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene_django/settings.py", line 74, in import_from_string
    module = importlib.import_module(module_path)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/eyal0803/Projects/cookbook/cookbook/schema.py", line 10, in <module>
    schema = graphene.Schema(query=Query)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene/types/schema.py", line 44, in __init__
    self.build_typemap()
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene/types/schema.py", line 105, in build_typemap
    self._type_map = TypeMap(initial_types, auto_camelcase=self.auto_camelcase, schema=self)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene/types/typemap.py", line 66, in __init__
    super(TypeMap, self).__init__(types)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphql/type/typemap.py", line 16, in __init__
    self.update(reduce(self.reducer, types, OrderedDict()))
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene/types/typemap.py", line 74, in reducer
    return self.graphene_reducer(map, type)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene/types/typemap.py", line 104, in graphene_reducer
    return GraphQLTypeMap.reducer(map, internal_type)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphql/type/typemap.py", line 79, in reducer
    field_map = type.fields
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphql/pyutils/cached_property.py", line 16, in __get__
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphql/type/definition.py", line 180, in fields
    return define_field_map(self, self._fields)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphql/type/definition.py", line 189, in define_field_map
    field_map = field_map()
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene/types/typemap.py", line 257, in construct_fields_for_type
    for arg_name, arg in field.args.items():
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene_django/filter/fields.py", line 23, in args
    return to_arguments(self._base_args or OrderedDict(), self.filtering_args)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene_django/filter/fields.py", line 44, in filtering_args
    return get_filtering_args_from_filterset(self.filterset_class, self.node_type)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene_django/filter/fields.py", line 38, in filterset_class
    self._filterset_class = get_filterset_class(self._provided_filterset_class, **meta)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene_django/filter/utils.py", line 28, in get_filterset_class
    return custom_filterset_factory(**meta)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene_django/filter/filterset.py", line 102, in custom_filterset_factory
    'Meta': meta_class
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/django_filters/filterset.py", line 90, in __new__
    new_class.base_filters = new_class.get_filters()
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/django_filters/filterset.py", line 316, in get_filters
    filters[field_name] = cls.filter_for_reverse_field(field, field_name)
  File "/home/eyal0803/Projects/cookbook/venv/lib/python3.6/site-packages/graphene_django/filter/filterset.py", line 66, in filter_for_reverse_field
    rel = f.field.rel
AttributeError: 'ForeignKey' object has no attribute 'rel'

Most helpful comment

This seems fixed on master: see here

If you just run pip install graphene-django you'll get 2.0.0 which does not have this fix. Use pip pinstall graphene-django==2.1rc1 or whatever the latest is.

All 3 comments

Same here :

Internal Server Error: /graphql
Traceback (most recent call last):
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene_django/settings.py", line 74, in import_from_string
    module = importlib.import_module(module_path)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/paul/Documents/pro/dev/local/cookbook/cookbook/schema.py", line 10, in <module>
    schema = graphene.Schema(query=Query)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene/types/schema.py", line 57, in __init__
    self.build_typemap()
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene/types/schema.py", line 123, in build_typemap
    schema=self
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene/types/typemap.py", line 69, in __init__
    super(TypeMap, self).__init__(types)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphql/type/typemap.py", line 16, in __init__
    self.update(reduce(self.reducer, types, OrderedDict()))
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene/types/typemap.py", line 77, in reducer
    return self.graphene_reducer(map, type)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene/types/typemap.py", line 107, in graphene_reducer
    return GraphQLTypeMap.reducer(map, internal_type)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphql/type/typemap.py", line 79, in reducer
    field_map = type.fields
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphql/pyutils/cached_property.py", line 16, in __get__
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphql/type/definition.py", line 180, in fields
    return define_field_map(self, self._fields)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphql/type/definition.py", line 189, in define_field_map
    field_map = field_map()
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene/types/typemap.py", line 260, in construct_fields_for_type
    for arg_name, arg in field.args.items():
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene_django/filter/fields.py", line 23, in args
    return to_arguments(self._base_args or OrderedDict(), self.filtering_args)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene_django/filter/fields.py", line 44, in filtering_args
    return get_filtering_args_from_filterset(self.filterset_class, self.node_type)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene_django/filter/fields.py", line 38, in filterset_class
    self._filterset_class = get_filterset_class(self._provided_filterset_class, **meta)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene_django/filter/utils.py", line 28, in get_filterset_class
    return custom_filterset_factory(**meta)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene_django/filter/filterset.py", line 95, in custom_filterset_factory
    'Meta': meta_class
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/django_filters/filterset.py", line 90, in __new__
    new_class.base_filters = new_class.get_filters()
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/django_filters/filterset.py", line 316, in get_filters
    filters[field_name] = cls.filter_for_reverse_field(field, field_name)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene_django/filter/filterset.py", line 60, in filter_for_reverse_field
    rel = f.field.rel
AttributeError: 'ForeignKey' object has no attribute 'rel'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/django/views/generic/base.py", line 63, in view
    self = cls(**initkwargs)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene_django/views.py", line 70, in __init__
    schema = graphene_settings.SCHEMA
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene_django/settings.py", line 116, in __getattr__
    val = perform_import(val, attr)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene_django/settings.py", line 60, in perform_import
    return import_from_string(val, setting_name)
  File "/home/paul/Documents/pro/dev/local/cookbook/lib/python3.6/site-packages/graphene_django/settings.py", line 78, in import_from_string
    raise ImportError(msg)
ImportError: Could not import 'cookbook.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: 'ForeignKey' object has no attribute 'rel'.

Same here. Is there a fix?

This seems fixed on master: see here

If you just run pip install graphene-django you'll get 2.0.0 which does not have this fix. Use pip pinstall graphene-django==2.1rc1 or whatever the latest is.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ZuluPro picture ZuluPro  路  3Comments

timothyjlaurent picture timothyjlaurent  路  3Comments

kayluhb picture kayluhb  路  4Comments

Northshoot picture Northshoot  路  4Comments

amiyatulu picture amiyatulu  路  3Comments