Search code examples
pythondjangodjango-modelsdjango-database

Can I use Django models to preform complex queries on a database?


I want to use django for a database driven app and I want to know if django models permit me to use complex query to fetch data.

I have for example this table inside a database:

Click to show image

I want to extract data resulting from this query.

SELECT name
FROM movies
WHERE year between 1995 AND 2001
AND rank between 6 and 9;

How can I do it with Django?


Solution

  • Movie.objects.filter(year__range(1995, 2001), rank__range(6, 9))

    You can use filters to limit querysets with Django.