Filter by ID for multiple data in Django

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 gives an error! How can i solve this? Based on the above tuple, I want to get all the students!

How to do it

+4
source share
3 answers

As stated in the stepnak statement for the request in the correct filter(id__in=id_tuple) syntax filter(id__in=id_tuple)

Django's documentation would be a great resource for getting this information. https://docs.djangoproject.com/en/dev/ref/models/querysets/#in

+10
source

You can use the search for the IN field, as @stepank suggests.

A bit more explanation: Django ORM uses special syntax to translate Python code into SQL statements. And just passing bool (if id defined) filter value is invalid syntax.

If you are new to Django, you can start with here . You can also refer to the full link . Turning to the documentation before asking is good practice. :)

+2
source

student = Students.objects.filter (id__in = id_tuple)

0
source

Source: https://habr.com/ru/post/1402888/


All Articles