Improved Django implementation

This commit is contained in:
Syrus Akbary 2016-08-15 23:51:51 -07:00
parent 13f9b658c1
commit f3dff38541

View File

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