How to order by a JSON from serializers py file in django rest framework

0 votes

I'm working with the django rest framework and I want make a order by to my json How I can make a order_by with django rest framework from the serializers.py file I have this in serializers.py

class EstablecimientoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Establecimiento
        depth = 1
        fields =   ('nombre','ciudad',)
    order_by = (
        ('nombre',)
    )

I have this order_by but this does nothing with the JSON

What is the correct way to do this order by in the JSON from serializers.py?

I have in views.py

class EstablecimientoViewSet(viewsets.ModelViewSet):
    queryset = Establecimiento.objects.order_by('nombre')
    serializer_class = EstablecimientoSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ('categoria','categoria__titulo',)

Then the order_by not work because I have this filter, How I can do to make the filter work well with order_by?

Jul 2, 2020 in Python by kartik
• 37,510 points
5,424 views

1 answer to this question.

0 votes

Hello @kartik,

There's an easy way, just override it explicitly by add a ordering line:

class EstablecimientoViewSet(viewsets.ModelViewSet):
    queryset = Establecimiento.objects
    serializer_class = EstablecimientoSerializer
    filter_backends = (filters.DjangoFilterBackend, filters.OrderingFilter)
    ordering = ('nombre',) #add this line
    filter_fields = ('categoria','categoria__titulo',)

Hope it works!!

Thank You!!

answered Jul 2, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

How to clear a database from the CLI with manage.py in Django?

Hello @kartik, To drop the database and run syncdb again. ...READ MORE

answered Aug 7, 2020 in Python by Niroj
• 82,880 points
2,422 views
0 votes
1 answer

How to Parse values from a JSON file?

import json from pprint import pprint with open('data.json') as ...READ MORE

answered Oct 15, 2018 in Python by Priyaj
• 58,090 points
1,674 views
0 votes
1 answer

how to read a JSON from a file?

You can use with statement with open('strings.json') as ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 58,090 points
5,250 views
0 votes
1 answer

How to create and read from a temporary file in Python?

Hi, there is a very simple solution ...READ MORE

answered Jan 29, 2019 in Python by Nymeria
• 3,560 points
1,753 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
2,041 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
1,190 views
0 votes
1 answer

How to return custom JSON in Django REST Framework?

Hello @kartik, There are 2 ways to custom ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,880 points
10,750 views
0 votes
1 answer

How to disable a method in a ViewSet, django-rest-framework

Hello @kartik, You could keep using viewsets.ModelViewSet and define http_method_names on your ...READ MORE

answered Jun 26, 2020 in Python by Niroj
• 82,880 points
10,110 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP