Add option for including foreignkey IDs

This commit is contained in:
Jacob Foster 2017-07-12 16:42:59 -05:00
parent d7c160c3b0
commit a33050a543

View File

@ -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