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.
Just use:
pip install django-postgres-tweaks
As the title says it already, these tools are designed to be used in Django projects/apps. So make
sure to add postgres_utils
or postgres_utils.apps.PostgresUtilsConfig
to the INSTALLED_APPS
list in your project's settings.py
!
That's it.
The lookups provided by this package/app are automatically loaded when the app is installed. You can go ahead and just use them like Django's built-in lookups, e.g.:
Pizza.objects.filter(name__noregex="[ ]+")
Assume you have a model called Pizza
with a name
field.
Like the DB functions provided by Django, e.g. in django.db.models.functions
, you need to
import them prior to usage. An example query looks like this:
Topping.objects\
.filter(name__contains="Onion")\
.annotate(onion_color=RegexpReplace("name", " *Onion$", ""))\
.values("name", "onion_color")\
.order_by("name")