mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-08 23:50:38 +03:00
Improved Django implementation
This commit is contained in:
parent
13f9b658c1
commit
f3dff38541
|
@ -9,28 +9,33 @@ from graphene.types.options import Options
|
||||||
from .utils import get_model_fields, is_valid_django_model, DJANGO_FILTER_INSTALLED
|
from .utils import get_model_fields, is_valid_django_model, DJANGO_FILTER_INSTALLED
|
||||||
from .registry import Registry, get_global_registry
|
from .registry import Registry, get_global_registry
|
||||||
from graphene.utils.is_base_type import is_base_type
|
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):
|
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
|
@staticmethod
|
||||||
def __new__(cls, name, bases, attrs):
|
def __new__(cls, name, bases, attrs):
|
||||||
|
@ -77,9 +82,14 @@ class DjangoObjectTypeMeta(ObjectTypeMeta):
|
||||||
|
|
||||||
options.django_fields = get_fields_in_type(
|
options.django_fields = get_fields_in_type(
|
||||||
ObjectType,
|
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
|
return cls
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user