扩展graphene-django FILTER_DEFAULTS不起作用

问题描述

我正在对项目中的django模型进行迭代的动态生成模式定义。

我的项目中包含一些自定义模型字段,这些字段需要将过滤器映射到它们,否则graphene-django将引发诸如以下的错误

AssertionError: MyModelSet resolved field 'custom' with 'exact' lookup to an unrecognized field type CustomField. Try adding an override to 'Meta.filter_overrides'. See: https://django-filter.readthedocs.io/en/master/ref/filterset.html#customise-filter-generation-with-filter-overrides

我已按照我的自定义字段->过滤器映射扩展了graphene_django.filter.filterset.GrapheneFilterSetMixin.FILTER_DEFAULT,如graphene-django github问题Extending global filter set overrides #1031中所建议。

这使上述错误消失了,但是似乎没有使用我映射到自定义字段的过滤器类。而是使用该过滤器继承的认django_filter ChoiceFilter:

class CustomChoiceField(forms.ChoiceField):
    def valid_value(self,value):
        """Check to see if the provided value is a valid choice."""
        print('VALIDATING !!!!!')

        text_value = str(value)
        for k,v in self.choices:
            if isinstance(v,(list,tuple)):
                # This is an optgroup,so look inside the group for options
                for k2,v2 in v:
                    if value == k2 or text_value == str(k2):
                        return True
            else:
                if value == k or text_value == str(k):
                    return True
        return False

class ChoiceFieldFilter(CustomChoiceField,ChoiceIteratorMixin):
    iterator = ChoiceIterator

class CustomChoiceFilter(ChoiceFilter):
    field_class = ChoiceFieldFilter

对服务器的运行查询不会从上述代码中打印VALIDATING !!!!!。使用调试器,我可以看到它确实使用了常用的django_filter ChoiceFilter。

但是它确实打印出了erorr:

 "message": "['{\"custom\": [{\"message\": \"Select a valid choice. VARIANT_BLUE is not one of the available choices.\",\"code\": \"invalid_choice\"}]}']",

当与

查询
  all_objects(first: 10,custom: "VARIANT_BLUE") {

我相信自定义过滤器会在graphene_django减少调用的过程中丢失,但是我无法找到位置。

有人知道如何在Graphene_django的自定义字段映射中使用自定义过滤器吗?

我正在使用带有中继(运行经过过滤的查询的DjangoFilterConnectionField)和django_filter的graphene_django。

编辑:

在graphene-django github页面上似乎存在一个常见问题,其中将枚举名称转换为大写会引起问题。我希望我在此处尝试使用自定义过滤器的方法可以解决该问题,并允许使用大写的枚举名称进行过滤。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)