db.models.functions.Length

class Length(expression, **extra) [source] Accepts a single text field or expression and returns the number of characters the value has. If the expression is null, then the length will also be null. Usage example: >>> # Get the length of the name and goes_by fields >>> from django.db.models.functions import Length >>> Author.objects.create(name='Margaret Smith') >>> author = Author.objects.annotate( ... name_length=Length('name'), ... goes_by_length=

db.models.functions.datetime.TruncSecond

class TruncSecond(expression, output_field=None, tzinfo=None, **extra) [source] kind = 'second' These are logically equivalent to Trunc('datetime_field', kind). They truncate all parts of the date up to kind and allow grouping or filtering datetimes with less precision. expression must have an output_field of DateTimeField. Usage example: >>> from datetime import date, datetime >>> from django.db.models import Count >>> from django.db.models.functions import (

db.models.functions.Greatest

class Greatest(*expressions, **extra) [source] New in Django 1.9. Accepts a list of at least two field names or expressions and returns the greatest value. Each argument must be of a similar type, so mixing text and numbers will result in a database error. Usage example: class Blog(models.Model): body = models.TextField() modified = models.DateTimeField(auto_now=True) class Comment(models.Model): body = models.TextField() modified = models.DateTimeField(auto_now=True)

db.models.functions.datetime.TruncYear

class TruncYear(expression, output_field=None, tzinfo=None, **extra) [source] kind = 'year'

db.models.functions.Lower

class Lower(expression, **extra) [source] Accepts a single text field or expression and returns the lowercase representation. It can also be registered as a transform as described in Length. Usage example: >>> from django.db.models.functions import Lower >>> Author.objects.create(name='Margaret Smith') >>> author = Author.objects.annotate(name_lower=Lower('name')).get() >>> print(author.name_lower) margaret smith Changed in Django 1.9: The ability to reg

db.models.functions.Now

class Now [source] New in Django 1.9. Returns the database server’s current date and time when the query is executed, typically using the SQL CURRENT_TIMESTAMP. Usage example: >>> from django.db.models.functions import Now >>> Article.objects.filter(published__lte=Now()) <QuerySet [<Article: How to Django>]> PostgreSQL considerations On PostgreSQL, the SQL CURRENT_TIMESTAMP returns the time that the current transaction started. Therefore for cross-database c

db.models.functions.Least

class Least(*expressions, **extra) [source] New in Django 1.9. Accepts a list of at least two field names or expressions and returns the least value. Each argument must be of a similar type, so mixing text and numbers will result in a database error. Warning The behavior of Least when one or more expression may be null varies 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

db.models.functions.datetime.Trunc

class Trunc(expression, kind, output_field=None, tzinfo=None, **extra) [source] 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 to filter or aggregate your data. For example, you can use Trunc to calculate the number of sales per day. Trunc takes a single expression, representing a DateField or DateTimeField, a kind representing a date part, and

db.models.functions.datetime.TruncMonth

class TruncMonth(expression, output_field=None, tzinfo=None, **extra) [source] kind = 'month' These are logically equivalent to Trunc('date_field', kind). They truncate all parts of the date up to kind which allows grouping or filtering dates with less precision. expression can have an output_field of either DateField or DateTimeField. Since DateFields don’t have a time component, only Trunc subclasses that deal with date-parts can be used with DateField: >>> from datetime impo

db.models.functions.datetime.TruncDay

class TruncDay(expression, output_field=None, tzinfo=None, **extra) [source] kind = 'day'