How to use Django database function expressions directly
Django has a many built-in database functions and a documented Func API for writing your own. Whilst writing a custom Func subclass may sometimes be necessary, I learnt that there’s many cases when you can instantiate Func with the necessary arguments to get what you need. For example, take the following model. from django.contrib.postgres.fields import ArrayField from django.db import models class Post(models.Model): tags = ArrayField(models.CharField(max_length=200)) Making use of the array_shuffle Postgres Array Function and an annotation, each of the post’s tags are in a randomised order. ...