J
J
Jekson2018-05-05 19:49:39
Django
Jekson, 2018-05-05 19:49:39

How to output different querisets in Django Rest framework?

Perhaps the question was formulated a little incorrectly. In general, I don’t understand how to display various collections of objects using DRF. I'm trying to understand by analogy with django views. I need to display a list of all places, a list of the most popular and a list of editors' choice.

def places_list(request):
        places = Places.objects.all()
        editor_places = Places.objects.filter(editor_choice = True )
        popular_places = Places.objects.filter(most_popular = True )

        return render (request, "places/places_list.html", {"places": places,
                                                           "editor_places": editor_places,
                                                           "popular_places": popular_places,
                                                        })

And then already in the template I display, let's say 3 tables with the parameters I need.
Making API
**serializer.py**
class PlaeceSerializer(serializers.ModelSerializer):
        class Meta:
            model = Places
            fields = (
                'main_photo',
                'name',               
            )

**views.py**
class PlacesListAPIView(ListAPIView):
        queryset = Places.objects.all()
        serializer_class = PlaeceSerializer

So I have a json collection containing all objects. How to make a selection correctly, in views.py, or how to work on the front with the received data? Share your experience, please. Preferably with an explanation of the essence of the process)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
planc, 2018-05-05
@Lepilov

www.django-rest-framework.org/api-guide/renderers
see for yourself how much data
you have, if a little, then all in one json and through renders display the link above
if there is a lot, then
www.django-rest-framework.org /api-guide/filtering
and let the front pull this url with parameters

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question