|
| 1 | +--- |
| 2 | +title : How to write RAW SQL queries in Django ORM |
| 3 | +sidebar_label : SQL Raw Query |
| 4 | +slug : how-to-write-raw-sql-queries-in-django-orm |
| 5 | +--- |
| 6 | + |
| 7 | +# How to Write Raw SQL Queries in Django ORM |
| 8 | + |
| 9 | +<SubHeading>Learn how to use Django ORM via Raw SQL Queries</SubHeading> |
| 10 | + |
| 11 | +In Django, you can execute raw SQL queries using the `django.db.connection` object or by using the `RawSQL` expression. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +> **Here's how to do both**: |
| 16 | +
|
| 17 | +## ✅ Method 1: Using `django.db.connection` |
| 18 | + |
| 19 | +You can execute raw SQL queries using the `django.db.connection` object. This method is useful when you need to perform custom queries that don't fit within the typical ORM structure. |
| 20 | + |
| 21 | +Here's an example: |
| 22 | + |
| 23 | +```python |
| 24 | +from django.db import connection |
| 25 | + |
| 26 | +# Your raw SQL query |
| 27 | +raw_sql = "SELECT * FROM your_table WHERE your_condition;" |
| 28 | + |
| 29 | +# Execute the query |
| 30 | +with connection.cursor() as cursor: |
| 31 | + cursor.execute(raw_sql) |
| 32 | + results = cursor.fetchall() |
| 33 | +``` |
| 34 | + |
| 35 | +Replace `your_table` with the name of your database table and `your_condition` with the desired SQL condition. |
| 36 | + |
| 37 | +## ✅ Method 2: Using `RawSQL` expression |
| 38 | + |
| 39 | +The `RawSQL` expression allows you to include raw SQL in a query while still using the Django ORM. Here's an example of how to use it: |
| 40 | + |
| 41 | +```python |
| 42 | +from django.db.models import RawSQL |
| 43 | +from yourapp.models import YourModel |
| 44 | + |
| 45 | +# Your raw SQL expression |
| 46 | +raw_sql = RawSQL("SELECT * FROM your_table WHERE your_condition;", []) |
| 47 | + |
| 48 | +# Use the raw SQL expression in a query |
| 49 | +results = YourModel.objects.raw(raw_sql) |
| 50 | +``` |
| 51 | + |
| 52 | +In this example: |
| 53 | + |
| 54 | +- `your_table` is the name of your database table. |
| 55 | +- `your_condition` is the SQL condition you want to apply. |
| 56 | + |
| 57 | +The `RawSQL` expression can be used in queries to include custom SQL expressions. |
| 58 | + |
| 59 | +Remember to replace `your_table`, `your_condition`, and `YourModel` with your actual table name, SQL condition, and model class, respectively. |
| 60 | + |
| 61 | +Using raw SQL queries should be done with caution, as it bypasses some of Django's built-in protections against SQL injection and can make your code less portable between different database backends. Always validate and sanitize user inputs when constructing raw SQL queries to prevent security risks. |
| 62 | + |
| 63 | +## ✅ Resources |
| 64 | + |
| 65 | +- 👉 Access [AppSeed](https://appseed.us/) and start your next project |
| 66 | +- 👉 [Deploy Projects on Aws, Azure and Digital Ocean](https://www.docs.deploypro.dev/) via **DeployPRO** |
| 67 | +- 👉 Create an amazing landing page with [Simpllo, an open-source site builder](https://www.simpllo.com/) |
| 68 | +- 👉 [Django App Generator](https://app-generator.dev/django/) - A 2nd generation App Builder |
0 commit comments