3π
β
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
:
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 as
ManyToManyField
, orForeignKey
) that start with a β+β.
π€yedpodtrzitko
0π
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]
π€Wilfried
- [Django]-Using mongoengine with models.ImageField
- [Django]-Best way to send files to GCS with Django
- [Django]-Difference between interactive Python console and Django's "manage.py shell"
- [Django]-Django β putting HTML tags inside blocks of text subject to translation
Source:stackexchange.com