def _base_args_setup(cls, only_fields=None, exclude_fields=None):
        if only_fields or exclude_fields:
            factory_kwargs = [
                # model
                cls._meta.model,
                # register
                gd_registry(),
                # fields
                only_fields or cls._meta.only_fields,
                # exclude
                exclude_fields or cls._meta.exclude_fields,
                # convert_choices_to_enum
                cls._meta.convert_choices_to_enum,
            ]
            django_fields = yank_fields_from_attrs(
                construct_fields(*factory_kwargs),
                _as=InputField,
            )
            return django_fields

        if cls.input_field_name:
            raise Exception(
                '{} type can not be empty use `only_fields` or '
                '`exclude_fields` to defined its fields. {}'.format(
                    cls.input_field_name, cls.__name__))
        return OrderedDict()
Пример #2
0
    def convert_serializer_field_to_enum(field):
        # enums require a name
        enum_name, cache_name = SerializerEnumConverter.get_cache_name_enum_name(
            field)

        registered_field = gd_registry().get_converted_field(cache_name)

        if registered_field:
            return registered_field.get_type()

        registered_field = gde_registry().get_type_for_enum(f"{enum_name}Enum")
        if registered_field:
            return registered_field

        cached_type = SerializerEnumConverter.serializers_enum_types_cache.get(
            str(cache_name), None)

        if cached_type:
            return cached_type

        ret_type = convert_choices_to_named_enum_with_descriptions(
            enum_name, field.choices)
        SerializerEnumConverter.serializers_enum_types_cache[str(
            cache_name)] = ret_type
        return ret_type
    def _get_output_fields(cls, model, only_fields, exclude_fields):
        output_type = gd_registry().get_type_for_model(
            model) or gde_registry().get_type_for_model(model)
        if not output_type:
            factory_kwargs = {
                "model": model,
                'only_fields': only_fields,
                'exclude_fields': exclude_fields,
                'skip_registry': False,
                'description': cls.output_field_description
            }
            output_type = factory_type("output", DjangoObjectType,
                                       **factory_kwargs)

        output = Field(
            output_type,
            description=cls.output_field_description
            or "Result can `{}` or `Null` if any error message(s)".format(
                model._meta.model_name.capitalize()))
        return output