在django过滤器后端传递模型的所有字段。

时间:2021-05-20 11:47:34

is there any way in which we can pass all the fields of a model to django filter backend without explicitly passing the names of fields in search_fields and filter_fields

有什么方法可以在不显式地传递search_fields和filter_fields中的字段名的情况下,将模型的所有字段传递给django过滤器后端

i have made a generic viewset which serializes all the fields of the model passed to it, but i am facing problem in applying generic filters to it

我制作了一个通用视图集,它序列化传递给它的模型的所有字段,但是我在向它应用通用过滤器时遇到了问题

for eg,

例如,

class UserListView(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = (filters.SearchFilter,)
    search_fields = ('username', 'email')

in the above code, we have explicitly passed search_fields but in my code, i can't pass the fields explicitely as everytime different model may be passed.

在上面的代码中,我们已经显式地通过了search_fields,但是在我的代码中,我不能明确地传递字段,因为每次都可以通过不同的模型。

2 个解决方案

#1


1  

I dont think it's wise to do it, as some fields can reveal sensitive information, but you can try to pass all fields from the model:

我不认为这样做是明智的,因为某些领域可以揭示敏感信息,但你可以试着从模型中传递所有领域:

class UserListView(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = (filters.SearchFilter,)
    search_fields = [f.name for f in User._meta.get_fields()]

Here are the docs for using get_fields:

下面是使用get_fields的文档:

Options.get_fields(include_parents=True, include_hidden=False)[source]

Returns a tuple of fields associated with a model. get_fields() accepts two parameters that can be used to control which fields are returned:

返回与模型关联的一组字段。get_fields()接受两个可用于控制返回哪些字段的参数:

  • include_parents True by default. Recursively includes fields defined on parent classes. If set to False, get_fields() will only search for fields declared directly on the current model. Fields from models that directly inherit from abstract models or proxy classes are considered to be local, not on the parent.
  • include_parents默认正确。递归地包含在父类上定义的字段。如果设置为False, get_fields()将只搜索直接在当前模型上声明的字段。直接从抽象模型或代理类继承的模型的字段被认为是本地的,而不是父类。
  • include_hidden False by default. If set to True, get_fields() will include fields that are used to back other field’s functionality. This will also include any fields that have a related_name (such as ManyToManyField, or ForeignKey) that start with a “+”.
  • include_hidden默认错误。如果设置为True, get_fields()将包含用于支持其他字段功能的字段。这还将包括任何以“+”开头的具有related_name(如ManyToManyField或ForeignKey)的字段。

#2


0  

https://docs.djangoproject.com/en/1.10/ref/models/meta/

https://docs.djangoproject.com/en/1.10/ref/models/meta/

class UserListView(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = (filters.SearchFilter,)
    search_fields = [field.name for field in User._meta.fields]  

#1


1  

I dont think it's wise to do it, as some fields can reveal sensitive information, but you can try to pass all fields from the model:

我不认为这样做是明智的,因为某些领域可以揭示敏感信息,但你可以试着从模型中传递所有领域:

class UserListView(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = (filters.SearchFilter,)
    search_fields = [f.name for f in User._meta.get_fields()]

Here are the docs for using get_fields:

下面是使用get_fields的文档:

Options.get_fields(include_parents=True, include_hidden=False)[source]

Returns a tuple of fields associated with a model. get_fields() accepts two parameters that can be used to control which fields are returned:

返回与模型关联的一组字段。get_fields()接受两个可用于控制返回哪些字段的参数:

  • include_parents True by default. Recursively includes fields defined on parent classes. If set to False, get_fields() will only search for fields declared directly on the current model. Fields from models that directly inherit from abstract models or proxy classes are considered to be local, not on the parent.
  • include_parents默认正确。递归地包含在父类上定义的字段。如果设置为False, get_fields()将只搜索直接在当前模型上声明的字段。直接从抽象模型或代理类继承的模型的字段被认为是本地的,而不是父类。
  • include_hidden False by default. If set to True, get_fields() will include fields that are used to back other field’s functionality. This will also include any fields that have a related_name (such as ManyToManyField, or ForeignKey) that start with a “+”.
  • include_hidden默认错误。如果设置为True, get_fields()将包含用于支持其他字段功能的字段。这还将包括任何以“+”开头的具有related_name(如ManyToManyField或ForeignKey)的字段。

#2


0  

https://docs.djangoproject.com/en/1.10/ref/models/meta/

https://docs.djangoproject.com/en/1.10/ref/models/meta/

class UserListView(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = (filters.SearchFilter,)
    search_fields = [field.name for field in User._meta.fields]