From f3dff38541bb43c20144eb476f1b91be8c7843fd Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Mon, 15 Aug 2016 23:51:51 -0700 Subject: [PATCH] Improved Django implementation --- graphene-django/graphene_django/types.py | 52 ++++++++++++++---------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/graphene-django/graphene_django/types.py b/graphene-django/graphene_django/types.py index 6fd9c941..f8aeae0b 100644 --- a/graphene-django/graphene_django/types.py +++ b/graphene-django/graphene_django/types.py @@ -9,28 +9,33 @@ from graphene.types.options import Options from .utils import get_model_fields, is_valid_django_model, DJANGO_FILTER_INSTALLED from .registry import Registry, get_global_registry from graphene.utils.is_base_type import is_base_type -from graphene.types.utils import get_fields_in_type +from graphene.types.utils import get_fields_in_type, merge + + +def construct_fields(options): + _model_fields = get_model_fields(options.model) + only_fields = options.only_fields + exclude_fields = options.exclude_fields + + fields = OrderedDict() + for field in _model_fields: + name = field.name + is_not_in_only = only_fields and name not in options.only_fields + is_already_created = name in options.fields + is_excluded = name in exclude_fields or is_already_created + if is_not_in_only or is_excluded: + # We skip this field if we specify only_fields and is not + # in there. Or when we exclude this field in exclude_fields + continue + converted = convert_django_field_with_choices(field, options.registry) + if not converted: + continue + fields[name] = converted + + return fields class DjangoObjectTypeMeta(ObjectTypeMeta): - def _construct_fields(cls, all_fields, options): - _model_fields = get_model_fields(options.model) - fields = OrderedDict() - for field in _model_fields: - name = field.name - is_not_in_only = options.only_fields and name not in options.only_fields - is_already_created = name in all_fields - is_excluded = name in options.exclude_fields or is_already_created - if is_not_in_only or is_excluded: - # We skip this field if we specify only_fields and is not - # in there. Or when we exclude this field in exclude_fields - continue - converted = convert_django_field_with_choices(field, options.registry) - if not converted: - continue - fields[name] = converted - - return fields @staticmethod def __new__(cls, name, bases, attrs): @@ -77,9 +82,14 @@ class DjangoObjectTypeMeta(ObjectTypeMeta): options.django_fields = get_fields_in_type( ObjectType, - cls._construct_fields(options.fields, options) + construct_fields(options) + ) + options.fields = merge( + options.interface_fields, + options.django_fields, + options.base_fields, + options.local_fields ) - options.fields.update(options.django_fields) return cls