id_tuple= (1,3,4,5,7,9)
students =Students.objects.filter(id in id_tuple)
for s in students :
s.name='_new_name_'
This code is giving error ! How can i solve it ?? based on the above tuple i want to get all student !
How to do that
As stepnak stated to do an in query the correct syntax is filter(id__in=id_tuple)
Django documentation would have been a great resource to get this information. https://docs.djangoproject.com/en/dev/ref/models/querysets/#in
You can use the IN field lookup as @stepank suggests.
A little more explanation: Django ORM uses a special syntax to translate python code to SQL statements. And simply passing a bool
(if id
is defined) value to filter
is not valid syntax.
If you are new to Django, you can get started from here. You can also refer to the complete reference. Referring to documentation before asking is a good practice. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With