在 django 过滤器后端传递模型的所有字段
pass all fields of model in django filter backend
有什么方法可以 pass all the fields of a model to django filter backend
而无需显式传递 search_fields 和 filter_fields
中的字段名称
我制作了一个通用视图集,它序列化传递给它的模型的所有字段,但我在向它应用通用过滤器时遇到问题
例如,
class UserListView(generics.ListAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
filter_backends = (filters.SearchFilter,)
search_fields = ('username', 'email')
在上面的代码中,我们已经显式传递了 search_fields
但在我的代码中,我无法显式传递字段,因为每次都可能传递不同的模型。
我认为这样做不明智,因为某些字段会泄露敏感信息,但您可以尝试传递模型中的所有字段:
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 使用 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:
- 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_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 “+”.
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]
有什么方法可以 pass all the fields of a model to django filter backend
而无需显式传递 search_fields 和 filter_fields
我制作了一个通用视图集,它序列化传递给它的模型的所有字段,但我在向它应用通用过滤器时遇到问题
例如,
class UserListView(generics.ListAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
filter_backends = (filters.SearchFilter,)
search_fields = ('username', 'email')
在上面的代码中,我们已经显式传递了 search_fields
但在我的代码中,我无法显式传递字段,因为每次都可能传递不同的模型。
我认为这样做不明智,因为某些字段会泄露敏感信息,但您可以尝试传递模型中的所有字段:
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 使用 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:
- include_parents
True
by default. Recursively includes fields defined on parent classes. If set toFalse
,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_hidden
False
by default. If set toTrue
,get_fields()
will include fields that are used to back other field’s functionality. This will also include any fields that have arelated_name
(such asManyToManyField
, orForeignKey
) that start with a “+”.
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]