24👍
Solution is to override filter_queryset
:
def filter_queryset(self, queryset):
queryset = super(InvoiceViewSet, self).filter_queryset(queryset)
return queryset.order_by('-published_date')
32👍
If your model does have an ordering it really will be reflected in the list view by default. I’d suggest overriding get_queryset()
and debugging the return result there, or else explicitly adding the ordering to the queryset.
For example:
queryset = Invoice.objects.all().order_by('-published_date')
Wondering if it’s possible you’ve configured a filter that’s overriding the ordering. Worth testing what happens if you turn all filters off. I see you have the filter_fields
attribute set, so assuming you’ve got something like this in your settings…
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',)
}
If you comment that out does that fix things?
- [Django]-*_set attributes on Django Models
- [Django]-Django Invalid HTTP_HOST header: 'testserver'. You may need to add u'testserver' to ALLOWED_HOSTS
- [Django]-How to do SELECT COUNT(*) GROUP BY and ORDER BY in Django?
28👍
For Django REST Framework you can use OrderingFilter.
from django_filters import DjangoFilterBackend
from rest_framework import viewsets, filters
class InvoiceViewSet(viewsets.ModelViewSet):
queryset = Invoice.objects.all()
serializer_class = InvoiceSerializer
filter_backends = (DjangoFilterBackend, filters.OrderingFilter)
# Explicitly specify which fields the API may be ordered against
ordering_fields = ('items', 'table', 'published_date')
# This will be used as the default ordering
ordering = ('-published_date')
- [Django]-How do I create a login API using Django Rest Framework?
- [Django]-Logging activity on Django's admin – Django
- [Django]-Convert Django Model object to dict with all of the fields intact
7👍
@Mirza Delic answer works but does not keep the ordering comming from request.QUERY_PARAMS.
class YOUR_VIEW_SET(viewsets.ModelViewSet):
#your code here
ordering_filter = OrderingFilter()
def filter_queryset(self, queryset):
queryset = super(YOUR_VIEW_SET, self).filter_queryset(queryset)
return self.ordering_filter.filter_queryset(self.request, queryset, self)
This works for me and for other people I hope.
- [Django]-How to go from django image field to PIL image and back?
- [Django]-How can I use Django permissions without defining a content type or model?
- [Django]-Pycharm error Django is not importable in this environment
0👍
If anyone’s coming here from google for ‘How to order django-filters queryset’.
There’s OrderingFilter in django-filters, which you can use to add the order_by functionality.
The example code from the docs looks like this :
class UserFilter(FilterSet):
account = CharFilter(field_name='username')
status = NumberFilter(field_name='status')
o = OrderingFilter(
# tuple-mapping retains order
fields=(
('username', 'account'),
('first_name', 'first_name'),
('last_name', 'last_name'),
),
# labels do not need to retain order
field_labels={
'username': 'User account',
}
)
class Meta:
model = User
fields = ['first_name', 'last_name']
Where o is the name of the query param.
So if you hit www.domain.com/o=username
. It will return the queryset ordered by username. If you want to order_by in descending order. Just do www.domain.com/o=-username
(notice the – before username).
- [Django]-Auto-create primary key used when not defining a primary key type warning in Django
- [Django]-Os.getcwd() vs os.path.abspath(os.path.dirname(__file__))
- [Django]-Not able to create super user with Django manage.py