17 Jul 2026
Django community aggregator: Community blog posts
Issue 346: Supporting the Triptych Project
17 Jul 2026 3:00pm GMT
15 Jul 2026
Django community aggregator: Community blog posts
Django: introducing django-orjson
Just as cars painted red are known to be faster, libraries implemented in Rust are also known to be faster. Today's example is orjson, a Rusty replacement for Python's built-in json module, boasting 10x faster serialization and 2x faster deserialization.
Such a library is great, but adopting it isn't easy, especially when your framework uses json in many different parts. To help Django developers adopt orjson, I have created django-orjson, which provides a whole bunch of drop-in replacements for Django and Django REST Framework (DRF) components backed by orjson.
For example, there's a version of JsonResponse:
from django_orjson.http import JsonResponse
def index(request):
return JsonResponse({"title": "Hello, world!"})
…a test client with matching test case classes:
from django_orjson.test import SimpleTestCase
class IndexTests(SimpleTestCase):
def test_index(self):
response = self.client.get("/", headers={"accept": "application/json"})
assert response.status_code == 200
# response.json() uses orjson to parse the response body
assert response.json() == {"title": "Hello, world!"}
…a version of Django's json_script template tag:
{% load django_orjson %}
{{ chart_data|json_script:"chart-data" }}
…and plenty more! All tested against the currently supported versions of Python and Django with 100% branch coverage.
While database queries tend to dominate the typical Django application's runtime, the time spent in serialization and deserialization can still be significant. That can make adopting orjson a nearly free performance win, which I hope django-orjson makes almost trivial for you.
Django proposal
After seeing the initial version of django-orjson, Paolo Melchiorre decided to push for adding orjson support to Django itself, in the new feature proposal Pluggable JSON serialization/deserialization backend. He made a thorough list of all the places in Django that could use orjson, and the proposal has gathered 14 thumbs-ups at the time of writing.
If you're interested in the topic of speeding up Django's JSON handling, check out the proposal and add your thoughts to the discussion.
15 Jul 2026 4:00am GMT
14 Jul 2026
Django community aggregator: Community blog posts
How to use a list/tuple/array in Django with a raw SQL cursor
This does not work:
from django.db import connection
list_of_values = [1, 2, 3]
with connection.cursor() as cursor:
cursor.execute("""
SELECT *
FROM my_model_table
WHERE some_value IN %s
""", [
tuple(list_of_values),
])
results = cursor.fetchall()
It will give you:
django.db.utils.ProgrammingError: syntax error at or near "'(1,2,3)'" LINE 4: WHERE id IN '(1,2,3)'
It used to work with psycopg v2. Now, in psycopg v3, you have to use the ANY operator. See "You cannot use IN %s with a tuple"
This will work:
from django.db import connection
list_of_values = [1, 2, 3]
with connection.cursor() as cursor:
cursor.execute(
"""
SELECT *
FROM my_model_table
WHERE some_value = ANY(%s)
""",
[
list_of_values,
],
)
results = cursor.fetchall()
Note the ANY(%s), and instead of a list that has a tuple, it's a list that has a list.
What About a List of Strings
Consider...
from django.db import connection
-list_of_values = [1, 2, 3]
+list_of_values = ['foo', 'bar', 'fiz']
with connection.cursor() as cursor:
cursor.execute(
"""
SELECT *
FROM my_model_table
WHERE some_value = ANY(%s)
""",
[
list_of_values,
],
)
results = cursor.fetchall()
That will result in:
django.db.utils.DataError: invalid input syntax for type integer: "foo"
LINE 4: WHERE some_value = ANY('{foo,bar,fiz}')
My solution was to rewrite the SQL string itself and treat each value as a parameter each. In other words, the SQL string, before being sent to cursor.execute(...) will contain something like this:
AND (
some_value = % OR
some_value = % OR
some_value = % OR
some_value = % OR
-- ...etc...
some_value = %
)
This will work and is safe:
from django.db import connection
list_of_values = ["foo", "bar", "fiz"]
with connection.cursor() as cursor:
cursor.execute(
f"""
SELECT *
FROM my_model_table
WHERE ({" OR ".join(["some_value = %s" for _ in list_of_values])})
""",
list_of_values,
)
results = cursor.fetchall()
14 Jul 2026 6:14pm GMT