Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django rest framework queryset doesn't order

i use model with Meta ordering = ['-published_date']

Now in view:

class InvoiceViewSet(viewsets.ModelViewSet):
    queryset = Invoice.objects.all()
    serializer_class = InvoiceSerializer
    filter_fields = ('table',)

And serializer:

class InvoiceSerializer(serializers.ModelSerializer):
    items = ItemSerializer(many=True, allow_add_remove=True)

    class Meta:
        model = Invoice
        fields = ('id',  'items', 'table', 'published_date')

But this ordering doesn't work, it shows me ordering ASC, and i need DESC, it doesn't affect order at all.

What am i doing wrong?

like image 564
Mirza Delic Avatar asked Jul 28 '14 01:07

Mirza Delic


People also ask

How do I filter Queryset in Django REST framework?

The simplest way to filter the queryset of any view that subclasses GenericAPIView is to override the .get_queryset() method. Overriding this method allows you to customize the queryset returned by the view in a number of different ways.

What is Queryset in Django REST framework?

queryset - The queryset used for model instance lookups when validating the field input. Relationships must either set a queryset explicitly, or set read_only=True . many - If applied to a to-many relationship, you should set this argument to True .

What is DjangoFilterBackend?

The DjangoFilterBackend class is used to filter the queryset based on a specified set of fields. This backend class automatically creates a FilterSet (django_filters. rest_framework. FilterSet) class for the given fields. We can also create our own FilterSet class with customized settings.


3 Answers

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?

like image 129
Tom Christie Avatar answered Oct 08 '22 19:10

Tom Christie


Solution is to override filter_queryset:

def filter_queryset(self, queryset):
    queryset = super(InvoiceViewSet, self).filter_queryset(queryset)
    return queryset.order_by('-published_date')
like image 23
Mirza Delic Avatar answered Oct 08 '22 19:10

Mirza Delic


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')
like image 23
shiva reddy Avatar answered Oct 08 '22 21:10

shiva reddy