Search code examples
djangodjango-modelssql-match-all

Django: performing a SQL match all in Django


I recently asked how to solve a simple SQL query. Turns out that there are many solutions.

After some benchmarking i think this is the best one:

SELECT DISTINCT Camera.*
FROM Camera c
     INNER JOIN cameras_features fc1 ON c.id = fc1.camera_id AND fc1.feature_id = 1
     INNER JOIN cameras_features fc2 ON c.id = fc2.camera_id AND fc2.feature_id = 2

Now, I've not clue how to perform this query with Django ORM.


Solution

  • If you need exactly this query you can execute this in django like raw sql. Here you can find about raw sql in django.

    It`s good to put your sql code into a custom manager. An example with manager and raw sql can be found here