I have 2 subcategorires of Tipo_Unidad, so I send via get the ID of tipo_unidad it depends of the checkbox selected but I have a problem, When I select the two categorires at the same time I send the ID in this format [1,2] and I don't know how to the for loop in my django view to obtain the query for each ID received.
This is part of my view:
q = request.GET.get('tipo_venta')
i = request.GET.getlist('id_tipo_unidad[]')
maxi = request.GET.get('Max')
mini = request.GET.get('Min')
if q is not None or i is not None or maxi is not None or mini is not None:
for var in i:
unidad = Unidad.objects.filter(id_tipo_unidad=var)
serializer = UnidadSerializer(unidad, many=True)
return Response(serializer.data)
else:
return Response({})
If I send only one ID the code works but if I send two it don't work, and the django console show this:
invalid literal for int() with base 10: '1,2'
There's a field lookup for what you're trying to do - you can do something like:
q = [1, 2]
unidad = Unidad.objects.filter(id__in=q)
That will give you all Unidad
objects who's id
is in the list q
. You can also span relationships with this:
q = [1, 2]
unidad = Unidad.objects.filter(some_other_model__id__in=q)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.