django rest framework - Adding filtering changes ordering -
I have a ModelViewSet, which I want to add filtering. My normal model looks like
class articles (models.Model): Date = = Model.Defined () language = Model.Charfild (max_long = 10) class meta: order = ['-' Date ']
and ModelViewSet (read only):
Category ArticleViewSet (viewsets.ReadOnlyModelViewSet): queryset = Article.objects.all () Serializer_class = Article Serializer
The article on the API has now ordered the date to be deed as I now hope to allow the filter on the language. I have set the filter backend in settings.py in DjangoFilterBackend
My Update ModelViewSet now looks like:
Category ArticleViewSet (viewsets.ReadOnlyModelViewSet): queryset = Article.objects .all () serializer_class = ArticleSerializer filter_fields = ['language']
This command changes the language ASC to order_by ('- date') adding
The query does not change anything. Adding ordering = ('-date',)
does not change anything. => How do I specify filtering and ordering (or just using default order while allowing filtering)?
Edit: By default, AutoFilterSet created in the rest of the framework comes with the current functionality: ... where order_by = true
and includes the Django- The filters are given in get_ordering_field
:
=> I think that is to create a FilterSet class:
class language filter (django_filters.FilterSet ): Square Meta: Model = Text Field = ['language'] Command_b = Model () ._ Meta.ordining Classes ArticleViewSet (viewsets.ReadOnlyModelViewSet): Queryset = Article.objects.all () serializer_class = ArticleSerializer filter_cla Ss = LanguageFilter
Is that correct? To maintain the default command, there seems to be a "lot" / verbose.
Your own FilterSet
, instead you have a ordering = ['-date']
or better: ordering = Article._meta.ordering
to restore the order (default) on your view
ordering
query parameter to override their default order.
Comments
Post a Comment