Django
- 2 ответа
- 0 вопросов
0
Вклад в тег
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)
<QuerySet []>
, то дело в заполненности БД: попросту не нашлось ни одного Tire, чтобы он соответствовал всем применённым фильтрам.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))