Database Functions

The classes documented below provide a way for users to use functions providedby the underlying database as annotations, aggregations, or filters in Django.Functions are also expressions, so they can be used andcombined with other expressions like aggregate functions.

We’ll be using the following model in examples of each function:

  1. class Author(models.Model):
  2. name = models.CharField(max_length=50)
  3. age = models.PositiveIntegerField(null=True, blank=True)
  4. alias = models.CharField(max_length=50, null=True, blank=True)
  5. goes_by = models.CharField(max_length=50, null=True, blank=True)

We don’t usually recommend allowing null=True for CharField since thisallows the field to have two “empty values”, but it’s important for theCoalesce example below.

Comparison and conversion functions

Cast

  • class Cast(expression, output_field)
  • Forces the result type of expression to be the one from output_field.

Usage example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Cast
  3. >>> Value.objects.create(integer=4)
  4. >>> value = Value.objects.annotate(as_float=Cast('integer', FloatField())).get()
  5. >>> print(value.as_float)
  6. 4.0

Coalesce

  • class Coalesce(*expressions, **extra)
  • Accepts a list of at least two field names or expressions and returns thefirst non-null value (note that an empty string is not considered a nullvalue). Each argument must be of a similar type, so mixing text and numberswill result in a database error.

Usage examples:

  1. >>> # Get a screen name from least to most public
  2. >>> from django.db.models import Sum, Value as V
  3. >>> from django.db.models.functions import Coalesce
  4. >>> Author.objects.create(name='Margaret Smith', goes_by='Maggie')
  5. >>> author = Author.objects.annotate(
  6. ... screen_name=Coalesce('alias', 'goes_by', 'name')).get()
  7. >>> print(author.screen_name)
  8. Maggie
  9.  
  10. >>> # Prevent an aggregate Sum() from returning None
  11. >>> aggregated = Author.objects.aggregate(
  12. ... combined_age=Coalesce(Sum('age'), V(0)),
  13. ... combined_age_default=Sum('age'))
  14. >>> print(aggregated['combined_age'])
  15. 0
  16. >>> print(aggregated['combined_age_default'])
  17. None

Warning

A Python value passed to Coalesce on MySQL may be converted to anincorrect type unless explicitly cast to the correct database type:

  1. >>> from django.db.models import DateTimeField
  2. >>> from django.db.models.functions import Cast, Coalesce
  3. >>> from django.utils import timezone
  4. >>> now = timezone.now()
  5. >>> Coalesce('updated', Cast(now, DateTimeField()))

Greatest

  • class Greatest(*expressions, **extra)
  • Accepts a list of at least two field names or expressions and returns thegreatest value. Each argument must be of a similar type, so mixing text andnumbers will result in a database error.

Usage example:

  1. class Blog(models.Model):
  2. body = models.TextField()
  3. modified = models.DateTimeField(auto_now=True)
  4.  
  5. class Comment(models.Model):
  6. body = models.TextField()
  7. modified = models.DateTimeField(auto_now=True)
  8. blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
  9.  
  10. >>> from django.db.models.functions import Greatest
  11. >>> blog = Blog.objects.create(body='Greatest is the best.')
  12. >>> comment = Comment.objects.create(body='No, Least is better.', blog=blog)
  13. >>> comments = Comment.objects.annotate(last_updated=Greatest('modified', 'blog__modified'))
  14. >>> annotated_comment = comments.get()

annotated_comment.last_updated will be the most recent of blog.modifiedand comment.modified.

Warning

The behavior of Greatest when one or more expression may be nullvaries between databases:

  • PostgreSQL: Greatest will return the largest non-null expression,or null if all expressions are null.
  • SQLite, Oracle, and MySQL: If any expression is null, Greatestwill return null.The PostgreSQL behavior can be emulated using Coalesce if you knowa sensible minimum value to provide as a default.

Least

  • class Least(*expressions, **extra)
  • Accepts a list of at least two field names or expressions and returns theleast value. Each argument must be of a similar type, so mixing text and numberswill result in a database error.

Warning

The behavior of Least when one or more expression may be nullvaries between databases:

  • PostgreSQL: Least will return the smallest non-null expression,or null if all expressions are null.
  • SQLite, Oracle, and MySQL: If any expression is null, Leastwill return null.The PostgreSQL behavior can be emulated using Coalesce if you knowa sensible maximum value to provide as a default.

NullIf

  • class NullIf(expression1, expression2)
  • New in Django 2.2:

Accepts two expressions and returns None if they are equal, otherwisereturns expression1.

Caveats on Oracle

Due to an Oracle convention, thisfunction returns the empty string instead of None when the expressionsare of type CharField.

Passing Value(None) to expression1 is prohibited on Oracle sinceOracle doesn’t accept NULL as the first argument.

Date functions

We’ll be using the following model in examples of each function:

  1. class Experiment(models.Model):
  2. start_datetime = models.DateTimeField()
  3. start_date = models.DateField(null=True, blank=True)
  4. start_time = models.TimeField(null=True, blank=True)
  5. end_datetime = models.DateTimeField(null=True, blank=True)
  6. end_date = models.DateField(null=True, blank=True)
  7. end_time = models.TimeField(null=True, blank=True)

Extract

  • class Extract(expression, lookup_name=None, tzinfo=None, **extra)
  • Extracts a component of a date as a number.

Takes an expression representing a DateField, DateTimeField,TimeField, or DurationField and a lookup_name, and returns the partof the date referenced by lookup_name as an IntegerField.Django usually uses the databases’ extract function, so you may use anylookup_name that your database supports. A tzinfo subclass, usuallyprovided by pytz, can be passed to extract a value in a specific timezone.

Given the datetime 2015-06-15 23:30:01.000321+00:00, the built-inlookup_names return:

  • “year”: 2015
  • “iso_year”: 2015
  • “quarter”: 2
  • “month”: 6
  • “day”: 15
  • “week”: 25
  • “week_day”: 2
  • “hour”: 23
  • “minute”: 30
  • “second”: 1If a different timezone like Australia/Melbourne is active in Django, thenthe datetime is converted to the timezone before the value is extracted. Thetimezone offset for Melbourne in the example date above is +10:00. The valuesreturned when this timezone is active will be the same as above except for:

  • “day”: 16

  • “week_day”: 3
  • “hour”: 9

week_day values

The week_day lookup_type is calculated differently from mostdatabases and from Python’s standard functions. This function will return1 for Sunday, 2 for Monday, through 7 for Saturday.

The equivalent calculation in Python is:

  1. >>> from datetime import datetime
  2. >>> dt = datetime(2015, 6, 15)
  3. >>> (dt.isoweekday() % 7) + 1
  4. 2

week values

The week lookup_type is calculated based on ISO-8601, i.e.,a week starts on a Monday. The first week of a year is the one thatcontains the year’s first Thursday, i.e. the first week has the majority(four or more) of its days in the year. The value returned is in the range1 to 52 or 53.

Each lookup_name above has a corresponding Extract subclass (listedbelow) that should typically be used instead of the more verbose equivalent,e.g. use ExtractYear(…) rather than Extract(…, lookup_name='year').

Usage example:

  1. >>> from datetime import datetime
  2. >>> from django.db.models.functions import Extract
  3. >>> start = datetime(2015, 6, 15)
  4. >>> end = datetime(2015, 7, 2)
  5. >>> Experiment.objects.create(
  6. ... start_datetime=start, start_date=start.date(),
  7. ... end_datetime=end, end_date=end.date())
  8. >>> # Add the experiment start year as a field in the QuerySet.
  9. >>> experiment = Experiment.objects.annotate(
  10. ... start_year=Extract('start_datetime', 'year')).get()
  11. >>> experiment.start_year
  12. 2015
  13. >>> # How many experiments completed in the same year in which they started?
  14. >>> Experiment.objects.filter(
  15. ... start_datetime__year=Extract('end_datetime', 'year')).count()
  16. 1

DateField extracts

  • class ExtractYear(expression, tzinfo=None, **extra)
    • lookup_name = 'year'
  • class ExtractIsoYear(expression, tzinfo=None, **extra)
  • New in Django 2.2:

Returns the ISO-8601 week-numbering year.

  • lookup_name = 'iso_year'
    • class ExtractMonth(expression, tzinfo=None, **extra)
  • lookup_name = 'month'
    • class ExtractDay(expression, tzinfo=None, **extra)
  • lookup_name = 'day'
    • class ExtractWeekDay(expression, tzinfo=None, **extra)
  • lookup_name = 'week_day'
    • class ExtractWeek(expression, tzinfo=None, **extra)
  • lookup_name = 'week'
    • class ExtractQuarter(expression, tzinfo=None, **extra)
  • lookup_name = 'quarter'
  • These are logically equivalent to Extract('datefield', lookupname). Eachclass is also a Transform registered on DateField and DateTimeFieldas (lookup_name), e.g. __year.

Since DateFields don’t have a time component, only Extract subclassesthat deal with date-parts can be used with DateField:

  1. >>> from datetime import datetime
  2. >>> from django.utils import timezone
  3. >>> from django.db.models.functions import (
  4. ... ExtractDay, ExtractMonth, ExtractQuarter, ExtractWeek,
  5. ... ExtractWeekDay, ExtractIsoYear, ExtractYear,
  6. ... )
  7. >>> start_2015 = datetime(2015, 6, 15, 23, 30, 1, tzinfo=timezone.utc)
  8. >>> end_2015 = datetime(2015, 6, 16, 13, 11, 27, tzinfo=timezone.utc)
  9. >>> Experiment.objects.create(
  10. ... start_datetime=start_2015, start_date=start_2015.date(),
  11. ... end_datetime=end_2015, end_date=end_2015.date())
  12. >>> Experiment.objects.annotate(
  13. ... year=ExtractYear('start_date'),
  14. ... isoyear=ExtractIsoYear('start_date'),
  15. ... quarter=ExtractQuarter('start_date'),
  16. ... month=ExtractMonth('start_date'),
  17. ... week=ExtractWeek('start_date'),
  18. ... day=ExtractDay('start_date'),
  19. ... weekday=ExtractWeekDay('start_date'),
  20. ... ).values('year', 'isoyear', 'quarter', 'month', 'week', 'day', 'weekday').get(
  21. ... end_date__year=ExtractYear('start_date'),
  22. ... )
  23. {'year': 2015, 'isoyear': 2015, 'quarter': 2, 'month': 6, 'week': 25,
  24. 'day': 15, 'weekday': 2}

DateTimeField extracts

In addition to the following, all extracts for DateField listed above mayalso be used on DateTimeFields .

  • class ExtractHour(expression, tzinfo=None, **extra)
    • lookup_name = 'hour'
  • class ExtractMinute(expression, tzinfo=None, **extra)
    • lookup_name = 'minute'
  • class ExtractSecond(expression, tzinfo=None, **extra)
    • lookup_name = 'second'
    • These are logically equivalent to Extract('datetimefield', lookupname).Each class is also a Transform registered on DateTimeField as(lookup_name), e.g. __minute.

DateTimeField examples:

  1. >>> from datetime import datetime
  2. >>> from django.utils import timezone
  3. >>> from django.db.models.functions import (
  4. ... ExtractDay, ExtractHour, ExtractMinute, ExtractMonth,
  5. ... ExtractQuarter, ExtractSecond, ExtractWeek, ExtractWeekDay,
  6. ... ExtractYear,
  7. ... )
  8. >>> start_2015 = datetime(2015, 6, 15, 23, 30, 1, tzinfo=timezone.utc)
  9. >>> end_2015 = datetime(2015, 6, 16, 13, 11, 27, tzinfo=timezone.utc)
  10. >>> Experiment.objects.create(
  11. ... start_datetime=start_2015, start_date=start_2015.date(),
  12. ... end_datetime=end_2015, end_date=end_2015.date())
  13. >>> Experiment.objects.annotate(
  14. ... year=ExtractYear('start_datetime'),
  15. ... isoyear=ExtractIsoYear('start_datetime'),
  16. ... quarter=ExtractQuarter('start_datetime'),
  17. ... month=ExtractMonth('start_datetime'),
  18. ... week=ExtractWeek('start_datetime'),
  19. ... day=ExtractDay('start_datetime'),
  20. ... weekday=ExtractWeekDay('start_datetime'),
  21. ... hour=ExtractHour('start_datetime'),
  22. ... minute=ExtractMinute('start_datetime'),
  23. ... second=ExtractSecond('start_datetime'),
  24. ... ).values(
  25. ... 'year', 'isoyear', 'month', 'week', 'day',
  26. ... 'weekday', 'hour', 'minute', 'second',
  27. ... ).get(end_datetime__year=ExtractYear('start_datetime'))
  28. {'year': 2015, 'isoyear': 2015, 'quarter': 2, 'month': 6, 'week': 25,
  29. 'day': 15, 'weekday': 2, 'hour': 23, 'minute': 30, 'second': 1}

When USE_TZ is True then datetimes are stored in the databasein UTC. If a different timezone is active in Django, the datetime is convertedto that timezone before the value is extracted. The example below converts tothe Melbourne timezone (UTC +10:00), which changes the day, weekday, and hourvalues that are returned:

  1. >>> import pytz
  2. >>> melb = pytz.timezone('Australia/Melbourne') # UTC+10:00
  3. >>> with timezone.override(melb):
  4. ... Experiment.objects.annotate(
  5. ... day=ExtractDay('start_datetime'),
  6. ... weekday=ExtractWeekDay('start_datetime'),
  7. ... hour=ExtractHour('start_datetime'),
  8. ... ).values('day', 'weekday', 'hour').get(
  9. ... end_datetime__year=ExtractYear('start_datetime'),
  10. ... )
  11. {'day': 16, 'weekday': 3, 'hour': 9}

Explicitly passing the timezone to the Extract function behaves in the sameway, and takes priority over an active timezone:

  1. >>> import pytz
  2. >>> melb = pytz.timezone('Australia/Melbourne')
  3. >>> Experiment.objects.annotate(
  4. ... day=ExtractDay('start_datetime', tzinfo=melb),
  5. ... weekday=ExtractWeekDay('start_datetime', tzinfo=melb),
  6. ... hour=ExtractHour('start_datetime', tzinfo=melb),
  7. ... ).values('day', 'weekday', 'hour').get(
  8. ... end_datetime__year=ExtractYear('start_datetime'),
  9. ... )
  10. {'day': 16, 'weekday': 3, 'hour': 9}

Now

  • class Now
  • Returns the database server’s current date and time when the query is executed,typically using the SQL CURRENT_TIMESTAMP.

Usage example:

  1. >>> from django.db.models.functions import Now
  2. >>> Article.objects.filter(published__lte=Now())
  3. <QuerySet [<Article: How to Django>]>

PostgreSQL considerations

On PostgreSQL, the SQL CURRENT_TIMESTAMP returns the time that thecurrent transaction started. Therefore for cross-database compatibility,Now() uses STATEMENT_TIMESTAMP instead. If you need the transactiontimestamp, use django.contrib.postgres.functions.TransactionNow.

Trunc

  • class Trunc(expression, kind, output_field=None, tzinfo=None, is_dst=None, **extra)
  • Truncates a date up to a significant component.

When you only care if something happened in a particular year, hour, or day,but not the exact second, then Trunc (and its subclasses) can be useful tofilter or aggregate your data. For example, you can use Trunc to calculatethe number of sales per day.

Trunc takes a single expression, representing a DateField,TimeField, or DateTimeField, a kind representing a date or timepart, and an output_field that’s either DateTimeField(),TimeField(), or DateField(). It returns a datetime, date, or timedepending on output_field, with fields up to kind set to their minimumvalue. If output_field is omitted, it will default to the output_fieldof expression. A tzinfo subclass, usually provided by pytz, can bepassed to truncate a value in a specific timezone.

The is_dst parameter indicates whether or not pytz should interpretnonexistent and ambiguous datetimes in daylight saving time. By default (whenis_dst=None), pytz raises an exception for such datetimes.

New in Django 3.0:The is_dst parameter was added.

Given the datetime 2015-06-15 14:30:50.000321+00:00, the built-in kindsreturn:

  • “year”: 2015-01-01 00:00:00+00:00
  • “quarter”: 2015-04-01 00:00:00+00:00
  • “month”: 2015-06-01 00:00:00+00:00
  • “week”: 2015-06-15 00:00:00+00:00
  • “day”: 2015-06-15 00:00:00+00:00
  • “hour”: 2015-06-15 14:00:00+00:00
  • “minute”: 2015-06-15 14:30:00+00:00
  • “second”: 2015-06-15 14:30:50+00:00If a different timezone like Australia/Melbourne is active in Django, thenthe datetime is converted to the new timezone before the value is truncated.The timezone offset for Melbourne in the example date above is +10:00. Thevalues returned when this timezone is active will be:

  • “year”: 2015-01-01 00:00:00+11:00

  • “quarter”: 2015-04-01 00:00:00+10:00
  • “month”: 2015-06-01 00:00:00+10:00
  • “week”: 2015-06-16 00:00:00+10:00
  • “day”: 2015-06-16 00:00:00+10:00
  • “hour”: 2015-06-16 00:00:00+10:00
  • “minute”: 2015-06-16 00:30:00+10:00
  • “second”: 2015-06-16 00:30:50+10:00The year has an offset of +11:00 because the result transitioned into daylightsaving time.

Each kind above has a corresponding Trunc subclass (listed below) thatshould typically be used instead of the more verbose equivalent,e.g. use TruncYear(…) rather than Trunc(…, kind='year').

The subclasses are all defined as transforms, but they aren’t registered withany fields, because the obvious lookup names are already reserved by theExtract subclasses.

Usage example:

  1. >>> from datetime import datetime
  2. >>> from django.db.models import Count, DateTimeField
  3. >>> from django.db.models.functions import Trunc
  4. >>> Experiment.objects.create(start_datetime=datetime(2015, 6, 15, 14, 30, 50, 321))
  5. >>> Experiment.objects.create(start_datetime=datetime(2015, 6, 15, 14, 40, 2, 123))
  6. >>> Experiment.objects.create(start_datetime=datetime(2015, 12, 25, 10, 5, 27, 999))
  7. >>> experiments_per_day = Experiment.objects.annotate(
  8. ... start_day=Trunc('start_datetime', 'day', output_field=DateTimeField())
  9. ... ).values('start_day').annotate(experiments=Count('id'))
  10. >>> for exp in experiments_per_day:
  11. ... print(exp['start_day'], exp['experiments'])
  12. ...
  13. 2015-06-15 00:00:00 2
  14. 2015-12-25 00:00:00 1
  15. >>> experiments = Experiment.objects.annotate(
  16. ... start_day=Trunc('start_datetime', 'day', output_field=DateTimeField())
  17. ... ).filter(start_day=datetime(2015, 6, 15))
  18. >>> for exp in experiments:
  19. ... print(exp.start_datetime)
  20. ...
  21. 2015-06-15 14:30:50.000321
  22. 2015-06-15 14:40:02.000123

DateField truncation

  • class TruncYear(expression, output_field=None, tzinfo=None, is_dst=None, **extra)
    • kind = 'year'
  • class TruncMonth(expression, output_field=None, tzinfo=None, is_dst=None, **extra)
    • kind = 'month'
  • class TruncWeek(expression, output_field=None, tzinfo=None, is_dst=None, **extra)
  • Truncates to midnight on the Monday of the week.

    • kind = 'week'
  • class TruncQuarter(expression, output_field=None, tzinfo=None, is_dst=None, **extra)
    • kind = 'quarter'
    • These are logically equivalent to Trunc('date_field', kind). They truncateall parts of the date up to kind which allows grouping or filtering dateswith less precision. expression can have an output_field of eitherDateField or DateTimeField.

Since DateFields don’t have a time component, only Trunc subclassesthat deal with date-parts can be used with DateField:

  1. >>> from datetime import datetime
  2. >>> from django.db.models import Count
  3. >>> from django.db.models.functions import TruncMonth, TruncYear
  4. >>> from django.utils import timezone
  5. >>> start1 = datetime(2014, 6, 15, 14, 30, 50, 321, tzinfo=timezone.utc)
  6. >>> start2 = datetime(2015, 6, 15, 14, 40, 2, 123, tzinfo=timezone.utc)
  7. >>> start3 = datetime(2015, 12, 31, 17, 5, 27, 999, tzinfo=timezone.utc)
  8. >>> Experiment.objects.create(start_datetime=start1, start_date=start1.date())
  9. >>> Experiment.objects.create(start_datetime=start2, start_date=start2.date())
  10. >>> Experiment.objects.create(start_datetime=start3, start_date=start3.date())
  11. >>> experiments_per_year = Experiment.objects.annotate(
  12. ... year=TruncYear('start_date')).values('year').annotate(
  13. ... experiments=Count('id'))
  14. >>> for exp in experiments_per_year:
  15. ... print(exp['year'], exp['experiments'])
  16. ...
  17. 2014-01-01 1
  18. 2015-01-01 2
  19.  
  20. >>> import pytz
  21. >>> melb = pytz.timezone('Australia/Melbourne')
  22. >>> experiments_per_month = Experiment.objects.annotate(
  23. ... month=TruncMonth('start_datetime', tzinfo=melb)).values('month').annotate(
  24. ... experiments=Count('id'))
  25. >>> for exp in experiments_per_month:
  26. ... print(exp['month'], exp['experiments'])
  27. ...
  28. 2015-06-01 00:00:00+10:00 1
  29. 2016-01-01 00:00:00+11:00 1
  30. 2014-06-01 00:00:00+10:00 1

DateTimeField truncation

  • class TruncDate(expression, **extra)
    • lookup_name = 'date'
    • output_field = DateField()
    • TruncDate casts expression to a date rather than using the built-in SQLtruncate function. It’s also registered as a transform on DateTimeField as__date.
  • class TruncTime(expression, **extra)

    • lookup_name = 'time'
    • output_field = TimeField()
    • TruncTime casts expression to a time rather than using the built-in SQLtruncate function. It’s also registered as a transform on DateTimeField as__time.
  • class TruncDay(expression, output_field=None, tzinfo=None, is_dst=None, **extra)

    • kind = 'day'
  • class TruncHour(expression, output_field=None, tzinfo=None, is_dst=None, **extra)
    • kind = 'hour'
  • class TruncMinute(expression, output_field=None, tzinfo=None, is_dst=None, **extra)
    • kind = 'minute'
  • class TruncSecond(expression, output_field=None, tzinfo=None, is_dst=None, **extra)
    • kind = 'second'
    • These are logically equivalent to Trunc('datetime_field', kind). Theytruncate all parts of the date up to kind and allow grouping or filteringdatetimes with less precision. expression must have an output_field ofDateTimeField.

Usage example:

  1. >>> from datetime import date, datetime
  2. >>> from django.db.models import Count
  3. >>> from django.db.models.functions import (
  4. ... TruncDate, TruncDay, TruncHour, TruncMinute, TruncSecond,
  5. ... )
  6. >>> from django.utils import timezone
  7. >>> import pytz
  8. >>> start1 = datetime(2014, 6, 15, 14, 30, 50, 321, tzinfo=timezone.utc)
  9. >>> Experiment.objects.create(start_datetime=start1, start_date=start1.date())
  10. >>> melb = pytz.timezone('Australia/Melbourne')
  11. >>> Experiment.objects.annotate(
  12. ... date=TruncDate('start_datetime'),
  13. ... day=TruncDay('start_datetime', tzinfo=melb),
  14. ... hour=TruncHour('start_datetime', tzinfo=melb),
  15. ... minute=TruncMinute('start_datetime'),
  16. ... second=TruncSecond('start_datetime'),
  17. ... ).values('date', 'day', 'hour', 'minute', 'second').get()
  18. {'date': datetime.date(2014, 6, 15),
  19. 'day': datetime.datetime(2014, 6, 16, 0, 0, tzinfo=<DstTzInfo 'Australia/Melbourne' AEST+10:00:00 STD>),
  20. 'hour': datetime.datetime(2014, 6, 16, 0, 0, tzinfo=<DstTzInfo 'Australia/Melbourne' AEST+10:00:00 STD>),
  21. 'minute': 'minute': datetime.datetime(2014, 6, 15, 14, 30, tzinfo=<UTC>),
  22. 'second': datetime.datetime(2014, 6, 15, 14, 30, 50, tzinfo=<UTC>)
  23. }

TimeField truncation

  • class TruncHour(expression, output_field=None, tzinfo=None, is_dst=None, **extra)
    • kind = 'hour'
  • class TruncMinute(expression, output_field=None, tzinfo=None, is_dst=None, **extra)
    • kind = 'minute'
  • class TruncSecond(expression, output_field=None, tzinfo=None, is_dst=None, **extra)
    • kind = 'second'
    • These are logically equivalent to Trunc('time_field', kind). They truncateall parts of the time up to kind which allows grouping or filtering timeswith less precision. expression can have an output_field of eitherTimeField or DateTimeField.

Since TimeFields don’t have a date component, only Trunc subclassesthat deal with time-parts can be used with TimeField:

  1. >>> from datetime import datetime
  2. >>> from django.db.models import Count, TimeField
  3. >>> from django.db.models.functions import TruncHour
  4. >>> from django.utils import timezone
  5. >>> start1 = datetime(2014, 6, 15, 14, 30, 50, 321, tzinfo=timezone.utc)
  6. >>> start2 = datetime(2014, 6, 15, 14, 40, 2, 123, tzinfo=timezone.utc)
  7. >>> start3 = datetime(2015, 12, 31, 17, 5, 27, 999, tzinfo=timezone.utc)
  8. >>> Experiment.objects.create(start_datetime=start1, start_time=start1.time())
  9. >>> Experiment.objects.create(start_datetime=start2, start_time=start2.time())
  10. >>> Experiment.objects.create(start_datetime=start3, start_time=start3.time())
  11. >>> experiments_per_hour = Experiment.objects.annotate(
  12. ... hour=TruncHour('start_datetime', output_field=TimeField()),
  13. ... ).values('hour').annotate(experiments=Count('id'))
  14. >>> for exp in experiments_per_hour:
  15. ... print(exp['hour'], exp['experiments'])
  16. ...
  17. 14:00:00 2
  18. 17:00:00 1
  19.  
  20. >>> import pytz
  21. >>> melb = pytz.timezone('Australia/Melbourne')
  22. >>> experiments_per_hour = Experiment.objects.annotate(
  23. ... hour=TruncHour('start_datetime', tzinfo=melb),
  24. ... ).values('hour').annotate(experiments=Count('id'))
  25. >>> for exp in experiments_per_hour:
  26. ... print(exp['hour'], exp['experiments'])
  27. ...
  28. 2014-06-16 00:00:00+10:00 2
  29. 2016-01-01 04:00:00+11:00 1

Math Functions

New in Django 2.2:

We’ll be using the following model in math function examples:

  1. class Vector(models.Model):
  2. x = models.FloatField()
  3. y = models.FloatField()

Abs

  • class Abs(expression, **extra)
  • Returns the absolute value of a numeric field or expression.

Usage example:

  1. >>> from django.db.models.functions import Abs
  2. >>> Vector.objects.create(x=-0.5, y=1.1)
  3. >>> vector = Vector.objects.annotate(x_abs=Abs('x'), y_abs=Abs('y')).get()
  4. >>> vector.x_abs, vector.y_abs
  5. (0.5, 1.1)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Abs
  3. >>> FloatField.register_lookup(Abs)
  4. >>> # Get vectors inside the unit cube
  5. >>> vectors = Vector.objects.filter(x__abs__lt=1, y__abs__lt=1)

ACos

  • class ACos(expression, **extra)
  • Returns the arccosine of a numeric field or expression. The expression valuemust be within the range -1 to 1.

Usage example:

  1. >>> from django.db.models.functions import ACos
  2. >>> Vector.objects.create(x=0.5, y=-0.9)
  3. >>> vector = Vector.objects.annotate(x_acos=ACos('x'), y_acos=ACos('y')).get()
  4. >>> vector.x_acos, vector.y_acos
  5. (1.0471975511965979, 2.6905658417935308)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import ACos
  3. >>> FloatField.register_lookup(ACos)
  4. >>> # Get vectors whose arccosine is less than 1
  5. >>> vectors = Vector.objects.filter(x__acos__lt=1, y__acos__lt=1)

ASin

  • class ASin(expression, **extra)
  • Returns the arcsine of a numeric field or expression. The expression value mustbe in the range -1 to 1.

Usage example:

  1. >>> from django.db.models.functions import ASin
  2. >>> Vector.objects.create(x=0, y=1)
  3. >>> vector = Vector.objects.annotate(x_asin=ASin('x'), y_asin=ASin('y')).get()
  4. >>> vector.x_asin, vector.y_asin
  5. (0.0, 1.5707963267948966)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import ASin
  3. >>> FloatField.register_lookup(ASin)
  4. >>> # Get vectors whose arcsine is less than 1
  5. >>> vectors = Vector.objects.filter(x__asin__lt=1, y__asin__lt=1)

ATan

  • class ATan(expression, **extra)
  • Returns the arctangent of a numeric field or expression.

Usage example:

  1. >>> from django.db.models.functions import ATan
  2. >>> Vector.objects.create(x=3.12, y=6.987)
  3. >>> vector = Vector.objects.annotate(x_atan=ATan('x'), y_atan=ATan('y')).get()
  4. >>> vector.x_atan, vector.y_atan
  5. (1.2606282660069106, 1.428638798133829)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import ATan
  3. >>> FloatField.register_lookup(ATan)
  4. >>> # Get vectors whose arctangent is less than 2
  5. >>> vectors = Vector.objects.filter(x__atan__lt=2, y__atan__lt=2)

ATan2

  • class ATan2(expression1, expression2, **extra)
  • Returns the arctangent of expression1 / expression2.

Usage example:

  1. >>> from django.db.models.functions import ATan2
  2. >>> Vector.objects.create(x=2.5, y=1.9)
  3. >>> vector = Vector.objects.annotate(atan2=ATan2('x', 'y')).get()
  4. >>> vector.atan2
  5. 0.9209258773829491

Ceil

  • class Ceil(expression, **extra)
  • Returns the smallest integer greater than or equal to a numeric field orexpression.

Usage example:

  1. >>> from django.db.models.functions import Ceil
  2. >>> Vector.objects.create(x=3.12, y=7.0)
  3. >>> vector = Vector.objects.annotate(x_ceil=Ceil('x'), y_ceil=Ceil('y')).get()
  4. >>> vector.x_ceil, vector.y_ceil
  5. (4.0, 7.0)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Ceil
  3. >>> FloatField.register_lookup(Ceil)
  4. >>> # Get vectors whose ceil is less than 10
  5. >>> vectors = Vector.objects.filter(x__ceil__lt=10, y__ceil__lt=10)

Cos

  • class Cos(expression, **extra)
  • Returns the cosine of a numeric field or expression.

Usage example:

  1. >>> from django.db.models.functions import Cos
  2. >>> Vector.objects.create(x=-8.0, y=3.1415926)
  3. >>> vector = Vector.objects.annotate(x_cos=Cos('x'), y_cos=Cos('y')).get()
  4. >>> vector.x_cos, vector.y_cos
  5. (-0.14550003380861354, -0.9999999999999986)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Cos
  3. >>> FloatField.register_lookup(Cos)
  4. >>> # Get vectors whose cosine is less than 0.5
  5. >>> vectors = Vector.objects.filter(x__cos__lt=0.5, y__cos__lt=0.5)

Cot

  • class Cot(expression, **extra)
  • Returns the cotangent of a numeric field or expression.

Usage example:

  1. >>> from django.db.models.functions import Cot
  2. >>> Vector.objects.create(x=12.0, y=1.0)
  3. >>> vector = Vector.objects.annotate(x_cot=Cot('x'), y_cot=Cot('y')).get()
  4. >>> vector.x_cot, vector.y_cot
  5. (-1.5726734063976826, 0.642092615934331)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Cot
  3. >>> FloatField.register_lookup(Cot)
  4. >>> # Get vectors whose cotangent is less than 1
  5. >>> vectors = Vector.objects.filter(x__cot__lt=1, y__cot__lt=1)

Degrees

  • class Degrees(expression, **extra)
  • Converts a numeric field or expression from radians to degrees.

Usage example:

  1. >>> from django.db.models.functions import Degrees
  2. >>> Vector.objects.create(x=-1.57, y=3.14)
  3. >>> vector = Vector.objects.annotate(x_d=Degrees('x'), y_d=Degrees('y')).get()
  4. >>> vector.x_d, vector.y_d
  5. (-89.95437383553924, 179.9087476710785)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Degrees
  3. >>> FloatField.register_lookup(Degrees)
  4. >>> # Get vectors whose degrees are less than 360
  5. >>> vectors = Vector.objects.filter(x__degrees__lt=360, y__degrees__lt=360)

Exp

  • class Exp(expression, **extra)
  • Returns the value of e (the natural logarithm base) raised to the power ofa numeric field or expression.

Usage example:

  1. >>> from django.db.models.functions import Exp
  2. >>> Vector.objects.create(x=5.4, y=-2.0)
  3. >>> vector = Vector.objects.annotate(x_exp=Exp('x'), y_exp=Exp('y')).get()
  4. >>> vector.x_exp, vector.y_exp
  5. (221.40641620418717, 0.1353352832366127)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Exp
  3. >>> FloatField.register_lookup(Exp)
  4. >>> # Get vectors whose exp() is greater than 10
  5. >>> vectors = Vector.objects.filter(x__exp__gt=10, y__exp__gt=10)

Floor

  • class Floor(expression, **extra)
  • Returns the largest integer value not greater than a numeric field orexpression.

Usage example:

  1. >>> from django.db.models.functions import Floor
  2. >>> Vector.objects.create(x=5.4, y=-2.3)
  3. >>> vector = Vector.objects.annotate(x_floor=Floor('x'), y_floor=Floor('y')).get()
  4. >>> vector.x_floor, vector.y_floor
  5. (5.0, -3.0)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Floor
  3. >>> FloatField.register_lookup(Floor)
  4. >>> # Get vectors whose floor() is greater than 10
  5. >>> vectors = Vector.objects.filter(x__floor__gt=10, y__floor__gt=10)

Ln

  • class Ln(expression, **extra)
  • Returns the natural logarithm a numeric field or expression.

Usage example:

  1. >>> from django.db.models.functions import Ln
  2. >>> Vector.objects.create(x=5.4, y=233.0)
  3. >>> vector = Vector.objects.annotate(x_ln=Ln('x'), y_ln=Ln('y')).get()
  4. >>> vector.x_ln, vector.y_ln
  5. (1.6863989535702288, 5.4510384535657)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Ln
  3. >>> FloatField.register_lookup(Ln)
  4. >>> # Get vectors whose value greater than e
  5. >>> vectors = Vector.objects.filter(x__ln__gt=1, y__ln__gt=1)

Log

  • class Log(expression1, expression2, **extra)
  • Accepts two numeric fields or expressions and returns the logarithm ofthe first to base of the second.

Usage example:

  1. >>> from django.db.models.functions import Log
  2. >>> Vector.objects.create(x=2.0, y=4.0)
  3. >>> vector = Vector.objects.annotate(log=Log('x', 'y')).get()
  4. >>> vector.log
  5. 2.0

Mod

  • class Mod(expression1, expression2, **extra)
  • Accepts two numeric fields or expressions and returns the remainder ofthe first divided by the second (modulo operation).

Usage example:

  1. >>> from django.db.models.functions import Mod
  2. >>> Vector.objects.create(x=5.4, y=2.3)
  3. >>> vector = Vector.objects.annotate(mod=Mod('x', 'y')).get()
  4. >>> vector.mod
  5. 0.8

Pi

  • class Pi(**extra)
  • Returns the value of the mathematical constant π.

Power

  • class Power(expression1, expression2, **extra)
  • Accepts two numeric fields or expressions and returns the value of the firstraised to the power of the second.

Usage example:

  1. >>> from django.db.models.functions import Power
  2. >>> Vector.objects.create(x=2, y=-2)
  3. >>> vector = Vector.objects.annotate(power=Power('x', 'y')).get()
  4. >>> vector.power
  5. 0.25

Radians

  • class Radians(expression, **extra)
  • Converts a numeric field or expression from degrees to radians.

Usage example:

  1. >>> from django.db.models.functions import Radians
  2. >>> Vector.objects.create(x=-90, y=180)
  3. >>> vector = Vector.objects.annotate(x_r=Radians('x'), y_r=Radians('y')).get()
  4. >>> vector.x_r, vector.y_r
  5. (-1.5707963267948966, 3.141592653589793)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Radians
  3. >>> FloatField.register_lookup(Radians)
  4. >>> # Get vectors whose radians are less than 1
  5. >>> vectors = Vector.objects.filter(x__radians__lt=1, y__radians__lt=1)

Round

  • class Round(expression, **extra)
  • Rounds a numeric field or expression to the nearest integer. Whether halfvalues are rounded up or down depends on the database.

Usage example:

  1. >>> from django.db.models.functions import Round
  2. >>> Vector.objects.create(x=5.4, y=-2.3)
  3. >>> vector = Vector.objects.annotate(x_r=Round('x'), y_r=Round('y')).get()
  4. >>> vector.x_r, vector.y_r
  5. (5.0, -2.0)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Round
  3. >>> FloatField.register_lookup(Round)
  4. >>> # Get vectors whose round() is less than 20
  5. >>> vectors = Vector.objects.filter(x__round__lt=20, y__round__lt=20)

Sign

  • class Sign(expression, **extra)
  • New in Django 3.0:

Returns the sign (-1, 0, 1) of a numeric field or expression.

Usage example:

  1. >>> from django.db.models.functions import Sign
  2. >>> Vector.objects.create(x=5.4, y=-2.3)
  3. >>> vector = Vector.objects.annotate(x_sign=Sign('x'), y_sign=Sign('y')).get()
  4. >>> vector.x_sign, vector.y_sign
  5. (1, -1)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Sign
  3. >>> FloatField.register_lookup(Sign)
  4. >>> # Get vectors whose signs of components are less than 0.
  5. >>> vectors = Vector.objects.filter(x__sign__lt=0, y__sign__lt=0)

Sin

  • class Sin(expression, **extra)
  • Returns the sine of a numeric field or expression.

Usage example:

  1. >>> from django.db.models.functions import Sin
  2. >>> Vector.objects.create(x=5.4, y=-2.3)
  3. >>> vector = Vector.objects.annotate(x_sin=Sin('x'), y_sin=Sin('y')).get()
  4. >>> vector.x_sin, vector.y_sin
  5. (-0.7727644875559871, -0.7457052121767203)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Sin
  3. >>> FloatField.register_lookup(Sin)
  4. >>> # Get vectors whose sin() is less than 0
  5. >>> vectors = Vector.objects.filter(x__sin__lt=0, y__sin__lt=0)

Sqrt

  • class Sqrt(expression, **extra)
  • Returns the square root of a nonnegative numeric field or expression.

Usage example:

  1. >>> from django.db.models.functions import Sqrt
  2. >>> Vector.objects.create(x=4.0, y=12.0)
  3. >>> vector = Vector.objects.annotate(x_sqrt=Sqrt('x'), y_sqrt=Sqrt('y')).get()
  4. >>> vector.x_sqrt, vector.y_sqrt
  5. (2.0, 3.46410)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Sqrt
  3. >>> FloatField.register_lookup(Sqrt)
  4. >>> # Get vectors whose sqrt() is less than 5
  5. >>> vectors = Vector.objects.filter(x__sqrt__lt=5, y__sqrt__lt=5)

Tan

  • class Tan(expression, **extra)
  • Returns the tangent of a numeric field or expression.

Usage example:

  1. >>> from django.db.models.functions import Tan
  2. >>> Vector.objects.create(x=0, y=12)
  3. >>> vector = Vector.objects.annotate(x_tan=Tan('x'), y_tan=Tan('y')).get()
  4. >>> vector.x_tan, vector.y_tan
  5. (0.0, -0.6358599286615808)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import FloatField
  2. >>> from django.db.models.functions import Tan
  3. >>> FloatField.register_lookup(Tan)
  4. >>> # Get vectors whose tangent is less than 0
  5. >>> vectors = Vector.objects.filter(x__tan__lt=0, y__tan__lt=0)

Text functions

Chr

  • class Chr(expression, **extra)
  • Accepts a numeric field or expression and returns the text representation ofthe expression as a single character. It works the same as Python’s chr()function.

Like Length, it can be registered as a transform on IntegerField.The default lookup name is chr.

Usage example:

  1. >>> from django.db.models.functions import Chr
  2. >>> Author.objects.create(name='Margaret Smith')
  3. >>> author = Author.objects.filter(name__startswith=Chr(ord('M'))).get()
  4. >>> print(author.name)
  5. Margaret Smith

Concat

  • class Concat(*expressions, **extra)
  • Accepts a list of at least two text fields or expressions and returns theconcatenated text. Each argument must be of a text or char type. If you wantto concatenate a TextField() with a CharField(), then be sure to tellDjango that the output_field should be a TextField(). Specifying anoutput_field is also required when concatenating a Value as in theexample below.

This function will never have a null result. On backends where a null argumentresults in the entire expression being null, Django will ensure that each nullpart is converted to an empty string first.

Usage example:

  1. >>> # Get the display name as "name (goes_by)"
  2. >>> from django.db.models import CharField, Value as V
  3. >>> from django.db.models.functions import Concat
  4. >>> Author.objects.create(name='Margaret Smith', goes_by='Maggie')
  5. >>> author = Author.objects.annotate(
  6. ... screen_name=Concat(
  7. ... 'name', V(' ('), 'goes_by', V(')'),
  8. ... output_field=CharField()
  9. ... )
  10. ... ).get()
  11. >>> print(author.screen_name)
  12. Margaret Smith (Maggie)

Left

  • class Left(expression, length, **extra)
  • Returns the first length characters of the given text field or expression.

Usage example:

  1. >>> from django.db.models.functions import Left
  2. >>> Author.objects.create(name='Margaret Smith')
  3. >>> author = Author.objects.annotate(first_initial=Left('name', 1)).get()
  4. >>> print(author.first_initial)
  5. M

Length

  • class Length(expression, **extra)
  • Accepts a single text field or expression and returns the number of charactersthe value has. If the expression is null, then the length will also be null.

Usage example:

  1. >>> # Get the length of the name and goes_by fields
  2. >>> from django.db.models.functions import Length
  3. >>> Author.objects.create(name='Margaret Smith')
  4. >>> author = Author.objects.annotate(
  5. ... name_length=Length('name'),
  6. ... goes_by_length=Length('goes_by')).get()
  7. >>> print(author.name_length, author.goes_by_length)
  8. (14, None)

It can also be registered as a transform. For example:

  1. >>> from django.db.models import CharField
  2. >>> from django.db.models.functions import Length
  3. >>> CharField.register_lookup(Length)
  4. >>> # Get authors whose name is longer than 7 characters
  5. >>> authors = Author.objects.filter(name__length__gt=7)

Lower

  • class Lower(expression, **extra)
  • Accepts a single text field or expression and returns the lowercaserepresentation.

It can also be registered as a transform as described in Length.

Usage example:

  1. >>> from django.db.models.functions import Lower
  2. >>> Author.objects.create(name='Margaret Smith')
  3. >>> author = Author.objects.annotate(name_lower=Lower('name')).get()
  4. >>> print(author.name_lower)
  5. margaret smith

LPad

  • class LPad(expression, length, fill_text=Value(' '), **extra)
  • Returns the value of the given text field or expression padded on the left sidewith fill_text so that the resulting value is length characters long.The default fill_text is a space.

Usage example:

  1. >>> from django.db.models import Value
  2. >>> from django.db.models.functions import LPad
  3. >>> Author.objects.create(name='John', alias='j')
  4. >>> Author.objects.update(name=LPad('name', 8, Value('abc')))
  5. 1
  6. >>> print(Author.objects.get(alias='j').name)
  7. abcaJohn

LTrim

  • class LTrim(expression, **extra)
  • Similar to Trim, but removes only leadingspaces.

MD5

  • class MD5(expression, **extra)
  • New in Django 3.0:

Accepts a single text field or expression and returns the MD5 hash of thestring.

It can also be registered as a transform as described in Length.

Usage example:

  1. >>> from django.db.models.functions import MD5
  2. >>> Author.objects.create(name='Margaret Smith')
  3. >>> author = Author.objects.annotate(name_md5=MD5('name')).get()
  4. >>> print(author.name_md5)
  5. 749fb689816b2db85f5b169c2055b247

Ord

  • class Ord(expression, **extra)
  • Accepts a single text field or expression and returns the Unicode code pointvalue for the first character of that expression. It works similar to Python’sord() function, but an exception isn’t raised if the expression is morethan one character long.

It can also be registered as a transform as described in Length.The default lookup name is ord.

Usage example:

  1. >>> from django.db.models.functions import Ord
  2. >>> Author.objects.create(name='Margaret Smith')
  3. >>> author = Author.objects.annotate(name_code_point=Ord('name')).get()
  4. >>> print(author.name_code_point)
  5. 77

Repeat

  • class Repeat(expression, number, **extra)
  • Returns the value of the given text field or expression repeated numbertimes.

Usage example:

  1. >>> from django.db.models.functions import Repeat
  2. >>> Author.objects.create(name='John', alias='j')
  3. >>> Author.objects.update(name=Repeat('name', 3))
  4. 1
  5. >>> print(Author.objects.get(alias='j').name)
  6. JohnJohnJohn

Replace

  • class Replace(expression, text, replacement=Value(''), **extra)
  • Replaces all occurrences of text with replacement in expression.The default replacement text is the empty string. The arguments to the functionare case-sensitive.

Usage example:

  1. >>> from django.db.models import Value
  2. >>> from django.db.models.functions import Replace
  3. >>> Author.objects.create(name='Margaret Johnson')
  4. >>> Author.objects.create(name='Margaret Smith')
  5. >>> Author.objects.update(name=Replace('name', Value('Margaret'), Value('Margareth')))
  6. 2
  7. >>> Author.objects.values('name')
  8. <QuerySet [{'name': 'Margareth Johnson'}, {'name': 'Margareth Smith'}]>

Reverse

  • class Reverse(expression, **extra)
  • New in Django 2.2:

Accepts a single text field or expression and returns the characters of thatexpression in reverse order.

It can also be registered as a transform as described in Length. Thedefault lookup name is reverse.

Usage example:

  1. >>> from django.db.models.functions import Reverse
  2. >>> Author.objects.create(name='Margaret Smith')
  3. >>> author = Author.objects.annotate(backward=Reverse('name')).get()
  4. >>> print(author.backward)
  5. htimS teragraM

Right

  • class Right(expression, length, **extra)
  • Returns the last length characters of the given text field or expression.

Usage example:

  1. >>> from django.db.models.functions import Right
  2. >>> Author.objects.create(name='Margaret Smith')
  3. >>> author = Author.objects.annotate(last_letter=Right('name', 1)).get()
  4. >>> print(author.last_letter)
  5. h

RPad

  • class RPad(expression, length, fill_text=Value(' '), **extra)
  • Similar to LPad, but pads on the rightside.

RTrim

  • class RTrim(expression, **extra)
  • Similar to Trim, but removes only trailingspaces.

SHA1, SHA224, SHA256, SHA384, and SHA512

  • class SHA1(expression, **extra)
  • class SHA224(expression, **extra)
  • class SHA256(expression, **extra)
  • class SHA384(expression, **extra)
  • class SHA512(expression, **extra)
  • New in Django 3.0:

Accepts a single text field or expression and returns the particular hash ofthe string.

They can also be registered as transforms as described in Length.

Usage example:

  1. >>> from django.db.models.functions import SHA1
  2. >>> Author.objects.create(name='Margaret Smith')
  3. >>> author = Author.objects.annotate(name_sha1=SHA1('name')).get()
  4. >>> print(author.name_sha1)
  5. b87efd8a6c991c390be5a68e8a7945a7851c7e5c

PostgreSQL

The pgcrypto extension must be installed. You can use theCryptoExtension migrationoperation to install it.

Oracle

Oracle doesn’t support the SHA224 function.

StrIndex

  • class StrIndex(string, substring, **extra)
  • Returns a positive integer corresponding to the 1-indexed position of the firstoccurrence of substring inside string, or 0 if substring is notfound.

Usage example:

  1. >>> from django.db.models import Value as V
  2. >>> from django.db.models.functions import StrIndex
  3. >>> Author.objects.create(name='Margaret Smith')
  4. >>> Author.objects.create(name='Smith, Margaret')
  5. >>> Author.objects.create(name='Margaret Jackson')
  6. >>> Author.objects.filter(name='Margaret Jackson').annotate(
  7. ... smith_index=StrIndex('name', V('Smith'))
  8. ... ).get().smith_index
  9. 0
  10. >>> authors = Author.objects.annotate(
  11. ... smith_index=StrIndex('name', V('Smith'))
  12. ... ).filter(smith_index__gt=0)
  13. <QuerySet [<Author: Margaret Smith>, <Author: Smith, Margaret>]>

Warning

In MySQL, a database table’s collation determineswhether string comparisons (such as the expression and substring ofthis function) are case-sensitive. Comparisons are case-insensitive bydefault.

Substr

  • class Substr(expression, pos, length=None, **extra)
  • Returns a substring of length length from the field or expression startingat position pos. The position is 1-indexed, so the position must be greaterthan 0. If length is None, then the rest of the string will be returned.

Usage example:

  1. >>> # Set the alias to the first 5 characters of the name as lowercase
  2. >>> from django.db.models.functions import Lower, Substr
  3. >>> Author.objects.create(name='Margaret Smith')
  4. >>> Author.objects.update(alias=Lower(Substr('name', 1, 5)))
  5. 1
  6. >>> print(Author.objects.get(name='Margaret Smith').alias)
  7. marga

Trim

  • class Trim(expression, **extra)
  • Returns the value of the given text field or expression with leading andtrailing spaces removed.

Usage example:

  1. >>> from django.db.models.functions import Trim
  2. >>> Author.objects.create(name=' John ', alias='j')
  3. >>> Author.objects.update(name=Trim('name'))
  4. 1
  5. >>> print(Author.objects.get(alias='j').name)
  6. John

Upper

  • class Upper(expression, **extra)
  • Accepts a single text field or expression and returns the uppercaserepresentation.

It can also be registered as a transform as described in Length.

Usage example:

  1. >>> from django.db.models.functions import Upper
  2. >>> Author.objects.create(name='Margaret Smith')
  3. >>> author = Author.objects.annotate(name_upper=Upper('name')).get()
  4. >>> print(author.name_upper)
  5. MARGARET SMITH

Window functions

There are a number of functions to use in aWindow expression for computing the rankof elements or the Ntile of some rows.

CumeDist

  • class CumeDist(*expressions, **extra)
  • Calculates the cumulative distribution of a value within a window or partition.The cumulative distribution is defined as the number of rows preceding orpeered with the current row divided by the total number of rows in the frame.

DenseRank

  • class DenseRank(*expressions, **extra)
  • Equivalent to Rank but does not have gaps.

FirstValue

  • class FirstValue(expression, **extra)
  • Returns the value evaluated at the row that’s the first row of the windowframe, or None if no such value exists.

Lag

  • class Lag(expression, offset=1, default=None, **extra)
  • Calculates the value offset by offset, and if no row exists there, returnsdefault.

default must have the same type as the expression, however, this isonly validated by the database and not in Python.

MariaDB and default

MariaDB doesn’t supportthe default parameter.

LastValue

  • class LastValue(expression, **extra)
  • Comparable to FirstValue, it calculates the last value in a givenframe clause.

Lead

  • class Lead(expression, offset=1, default=None, **extra)
  • Calculates the leading value in a given frame. Bothoffset and default are evaluated with respect to the current row.

default must have the same type as the expression, however, this isonly validated by the database and not in Python.

MariaDB and default

MariaDB doesn’t supportthe default parameter.

NthValue

  • class NthValue(expression, nth=1, **extra)
  • Computes the row relative to the offset nth (must be a positive value)within the window. Returns None if no row exists.

Some databases may handle a nonexistent nth-value differently. For example,Oracle returns an empty string rather than None for character-basedexpressions. Django doesn’t do any conversions in these cases.

Ntile

  • class Ntile(num_buckets=1, **extra)
  • Calculates a partition for each of the rows in the frame clause, distributingnumbers as evenly as possible between 1 and num_buckets. If the rows don’tdivide evenly into a number of buckets, one or more buckets will be representedmore frequently.

PercentRank

  • class PercentRank(*expressions, **extra)
  • Computes the percentile rank of the rows in the frame clause. Thiscomputation is equivalent to evaluating:
  1. (rank - 1) / (total rows - 1)

The following table explains the calculation for the percentile rank of a row:

Row #ValueRankCalculationPercent Rank
1151(1-1)/(7-1)0.0000
2202(2-1)/(7-1)0.1666
3202(2-1)/(7-1)0.1666
4202(2-1)/(7-1)0.1666
5305(5-1)/(7-1)0.6666
6305(5-1)/(7-1)0.6666
7407(7-1)/(7-1)1.0000

Rank

  • class Rank(*expressions, **extra)
  • Comparable to RowNumber, this function ranks rows in the window. Thecomputed rank contains gaps. Use DenseRank to compute rank withoutgaps.

RowNumber

  • class RowNumber(*expressions, **extra)
  • Computes the row number according to the ordering of either the frame clauseor the ordering of the whole query if there is no partitioning of thewindow frame.