About Querying Items Containing a Specific Value in a JSON Field

To query records by a specific key–value pair inside a nested JSON field, you can convert the value to a string and search it using icontains, like this:

image = MediaImage.objects.get(pk=1)
related_articles = Article.objects.filter(
    content_json__icontains=f'"uuid": "{image.uuid}"'
)

Note that the double quotes and the space matter here.

Tips and Tricks Programming Databases Django 6.x Django 5.2 Django 4.2 PostgreSQL

Django/Python Consulting

If you have a specific Django challenge or integration you'd like to solve, I'd be happy to help. Book a free 30-minute call to discuss your project, see if we're a good fit, and explore the best approach for your needs. After the call, you'll receive a tailored cost estimate based on what we discuss.