Skip to content

Tag: django

SQL double many-to-many relations with only two tables

Having two tables, one of them is called Tasks and the other one Relations where every task in Tasks might have 0-to-many predecessors and similarly, 0-to-many successors that relations among tasks are stored in the Relations-table. So, if Task_1 is predecessor of Task_2 and Task_2 is predecessor of Task_3 th…

Transforming SQL Query to Django Expression

Assuming I have the following Django models and the according SQL-Tables with some data. I have simplified the models so that it is clearer. UserAnswer: id answer partquestion_id 1 667 1 PartQuestion: id question_id part_id 1 1 1 Solution: id content question_id 1 667 1 2 85 2 I want to get all User answers w…

Extract JSON content in Metabase SQL query

Using: Django==2.2.24, Python=3.6, PostgreSQL is underlying DB Working with Django ORM, I can easily make all sort of queries, but I started using Metabase, and my SQL might be a bit rusty. The problem: I am trying to get a count of the items in a list, under a key in a dictionary, stored as a JSONField: Exam…

Django ORM Left Join onto same table

I have a custom permissions system in a django project that can link any user to any specific model instance to grant permission on that object. It is more complex than this, but boiled down: I want to query for all permissions for any user, that are linked to the same content_object(s) that a particular user…