diff --git a/graphene_django/types.py b/graphene_django/types.py index 684863a..de8abd4 100644 --- a/graphene_django/types.py +++ b/graphene_django/types.py @@ -1,12 +1,13 @@ from collections import OrderedDict +from django.db.models import ForeignKey from django.utils.functional import SimpleLazyObject from graphene import Field from graphene.relay import Connection, Node from graphene.types.objecttype import ObjectType, ObjectTypeOptions from graphene.types.utils import yank_fields_from_attrs -from .converter import convert_django_field_with_choices +from .converter import convert_django_field_with_choices, convert_field_to_id from .registry import Registry, get_global_registry from .utils import (DJANGO_FILTER_INSTALLED, get_model_fields, is_valid_django_model) @@ -30,6 +31,15 @@ def construct_fields(model, registry, only_fields, exclude_fields): converted = convert_django_field_with_choices(field, registry) fields[name] = converted + attname = getattr(field, 'attname', '') + add_foreignkey_attname = all([ + isinstance(field, ForeignKey), + options.include_foreignkey_ids, + attname not in fields, + ]) + if add_foreignkey_attname: + fields[field.attname] = convert_field_to_id(field) + return fields