PostgreSQL lookups and functions for Django apps

How often have you had the impression that Django was not providing all the lookup expressions and functions for your queries? Probably not that often, but now here is a small collection that I consider quite useful.

You will get started quite quickly!

In this documentation I will assume the following data model to illustrate code examples:

digraph model_graph { // Dotfile by Django-Extensions graph_models // Created: 2020-02-13 08:41 // Cli Options: testapp fontname = "Roboto" fontsize = 8 splines = true rankdir = RL node [ fontname = "Roboto" fontsize = 8 shape = "plaintext" ] edge [ fontname = "Roboto" fontsize = 8 ] // Labels testapp_models_BaseNameModel [label=< <TABLE BGCOLOR="white" BORDER="1" CELLBORDER="0" CELLSPACING="0"> <TR><TD COLSPAN="2" CELLPADDING="5" ALIGN="CENTER" BGCOLOR="#1b563f"> <FONT FACE="Roboto" COLOR="white" POINT-SIZE="10"><B> BaseNameModel </B></FONT></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"> <FONT FACE="Roboto">name</FONT> </TD><TD ALIGN="LEFT"> <FONT FACE="Roboto">CharField</FONT> </TD></TR> </TABLE> >] testapp_models_Topping [label=< <TABLE BGCOLOR="white" BORDER="1" CELLBORDER="0" CELLSPACING="0"> <TR><TD COLSPAN="2" CELLPADDING="5" ALIGN="CENTER" BGCOLOR="#1b563f"> <FONT FACE="Roboto" COLOR="white" POINT-SIZE="10"><B> Topping<BR/>&lt;<FONT FACE="Roboto"><I>BaseNameModel</I></FONT>&gt; </B></FONT></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"> <FONT FACE="Roboto"><B>id</B></FONT> </TD><TD ALIGN="LEFT"> <FONT FACE="Roboto"><B>AutoField</B></FONT> </TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"> <FONT FACE="Roboto"><I>name</I></FONT> </TD><TD ALIGN="LEFT"> <FONT FACE="Roboto"><I>CharField</I></FONT> </TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"> <FONT FACE="Roboto">vegan</FONT> </TD><TD ALIGN="LEFT"> <FONT FACE="Roboto">BooleanField</FONT> </TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"> <FONT FACE="Roboto">veggy</FONT> </TD><TD ALIGN="LEFT"> <FONT FACE="Roboto">BooleanField</FONT> </TD></TR> </TABLE> >] testapp_models_Pizza [label=< <TABLE BGCOLOR="white" BORDER="1" CELLBORDER="0" CELLSPACING="0"> <TR><TD COLSPAN="2" CELLPADDING="5" ALIGN="CENTER" BGCOLOR="#1b563f"> <FONT FACE="Roboto" COLOR="white" POINT-SIZE="10"><B> Pizza<BR/>&lt;<FONT FACE="Roboto"><I>BaseNameModel</I></FONT>&gt; </B></FONT></TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"> <FONT FACE="Roboto"><B>id</B></FONT> </TD><TD ALIGN="LEFT"> <FONT FACE="Roboto"><B>AutoField</B></FONT> </TD></TR> <TR><TD ALIGN="LEFT" BORDER="0"> <FONT FACE="Roboto"><I>name</I></FONT> </TD><TD ALIGN="LEFT"> <FONT FACE="Roboto"><I>CharField</I></FONT> </TD></TR> </TABLE> >] // Relations testapp_models_Topping -> testapp_models_BaseNameModel [label=" abstract\ninheritance"] [arrowhead=empty, arrowtail=none, dir=both]; testapp_models_Pizza -> testapp_models_Topping [label=" toppings (pizza)"] [arrowhead=dot arrowtail=dot, dir=both]; testapp_models_Pizza -> testapp_models_BaseNameModel [label=" abstract\ninheritance"] [arrowhead=empty, arrowtail=none, dir=both]; }

Indices and tables