Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: does the ORM support the SQL "IN" operator?

Does the Django ORM support the SQL IN operator? Something like:

SELECT * FROM user WHERE id IN (1, 5, 34, 567, 229) 

How do I use the Django ORM to make a query like that?

Thanks.

like image 782
Continuation Avatar asked Mar 30 '11 04:03

Continuation


People also ask

How does ORM work in Django?

One of the most powerful features of Django is its Object-Relational Mapper (ORM), which enables you to interact with your database, like you would with SQL. In fact, Django's ORM is just a pythonical way to create SQL to query and manipulate your database and get results in a pythonic fashion.

Which operators can be used to access the fields in the object by Django ORM?

You use a period (.) to access the fields in the Django ORM object.

What is are applicable for Django ORM?

The Django web framework includes a default object-relational mapping layer (ORM) that can be used to interact with application data from various relational databases such as SQLite, PostgreSQL and MySQL. The Django ORM is an implementation of the object-relational mapping (ORM) concept.

What is Django ORM and its benefits over raw SQL?

The Django ORM provides many tools to express queries without writing raw SQL. For example: The QuerySet API is extensive. You can annotate and aggregate using many built-in database functions. Beyond those, you can create custom query expressions.


1 Answers

in

User.objects.filter(id__in=[1, 5, 34, 567, 229])  print _.query SELECT <fields> FROM "auth_user" WHERE "auth_user"."id" IN (1, 5, 34, 567, 229) 
like image 153
Yuji 'Tomita' Tomita Avatar answered Oct 30 '22 18:10

Yuji 'Tomita' Tomita