Prologue:
This is a question arising often in SO:
- Equivalent of PostGIS ST_MakeValid in Django GEOS
- Geodjango: How to Buffer From Point
- Get random point from django PolygonField
- Django custom for complex Func (sql function)
and can be applied to the above as well as in the following:
I wanted to compose an example on SO Documentation but since it got shut down on August 8, 2017, I will follow the suggestion of this widely upvoted and discussed meta answer and write my example as a self-answered post.
Of course, I would be more than happy to see any different approach as well!!
Question:
Django/GeoDjango has some database functions like Lower() or MakeValid() which can be used like this:
Author.objects.create(name='Margaret Smith')
author = Author.objects.annotate(name_lower=Lower('name')).get()
print(author.name_lower)
Is there any way to use and/or create my own custom database function based on existing database functions like:
Position()(MySQL)TRIM()(SQLite)ST_MakePoint()(PostgreSQL with PostGIS)
How can I apply/use those functions in Django/GeoDjango ORM?
Django provides the
Func()expression to facilitate the calling of database functions in a queryset:There are 2 options on how to use a database function in Django/GeoDjango ORM:
For convenience, let us assume that the model is named
MyModeland that the substring is stored in a variable namedsubst:Use
Func()to call the function directly:We will also need the following to make our queries work:
F()which allows the execution of arithmetic operations on and between model fields.Value()which will sanitize any given value (why is this important?)The query:
Create your own database function extending
Func:We can extend
Funcclass to create our own database functions:and use it in a query:
GeoDjango Appendix:
In GeoDjango, in order to import a GIS related function (like
PostGIS'sTransformfunction) theFunc()method must be replaced byGeoFunc(), but it is essentially used under the same principles:There are more complex cases of
GeoFuncusage and an interesting use case has emerged here: How to calculate Frechet Distance in Django?Generalize custom database function Appendix:
In case that you want to create a custom database function (Option 2) and you want to be able to use it with any database without knowing it beforehand, you can use
Func'sas_<database-name>method, provided that the function you want to use exists in every database: