E
E
Elvinchik2022-02-01 23:48:16
Python
Elvinchik, 2022-02-01 23:48:16

How to turn QuerySet into JSON?

there is this code:

def post(self, request):
        fields = ["en_mm", "height", "diametr", "season", "cartype"]
        qs = TireModel.objects.all()
        for field in fields:
            if request.POST.get(field) != "" and request.POST.get(field) != "All":
                qs = qs.filter(**{field: request.POST.get(field)})

        print(qs) # Выводит <QuerySet []>
        
        return JsonResponse({"data": "1"}, status=200)


How to make from qs JSON?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
DoomAndGloom, 2022-02-02
@DoomAndGloom

1. What you really need is not JSON, but a dictionary. Do not confuse these things :) JSON is a string formatted in such a way that it is very similar to a Python dictionary. It is used not only in Python, but in many other places.
2. If you want to find an answer, then the word "serialize" (eng. "serialize") is useful for Google, this is about what you need.
Usually they use DRF for this, here is a serialization tutorial in it
. The result will be something like this:

from rest_framework import serializers

class TireSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tire
        fields = ["en_mm", "height", "diametr", "season", "cartype"]

def post(self, request):
    fields = ["en_mm", "height", "diametr", "season", "cartype"]
    qs = TireModel.objects.all()
    for field in fields:
        if request.POST.get(field) != "" and request.POST.get(field) != "All":
            qs = qs.filter(**{field: request.POST.get(field)})
    serializer = TireSerializer(qs, many=True)
    print(serializer.data)

3. If it’s embarrassing what it displays <QuerySet []>, then the point is that the database is full: there simply wasn’t a single Tire to match all the applied filters.
4. You can do without DRF, for example, here is what Django offers out of the box , or you can do everything simply by hand:
from rest_framework import serializers

def serialize_tires(tires):
    return [
        {"en_mm": tire.en_mm,
         "height": tire.height,
         "diametr": tire.diametr,
         "season": tire.season,
         "cartype": tire.cartype} for tire in tires]

def post(self, request):
    fields = ["en_mm", "height", "diametr", "season", "cartype"]
    qs = TireModel.objects.all()
    for field in fields:
        if request.POST.get(field) != "" and request.POST.get(field) != "All":
            qs = qs.filter(**{field: request.POST.get(field)})
    print(serialize_tires(tires))

This method is suitable if there is not very much serialization in the project, so as not to drag another framework into the project for the sake of a couple of lines of code. But if it is planned to be used everywhere in the project, it is better to take DRF.

A
Antonio Solo, 2022-02-02
@solotony

use values_list for QuerySet
and then convert the result to JSON

T
trankov, 2022-02-03
@trankov

1. import json
2. Get dictionary from queryset
3.json.dumps(словарь)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question