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:
1 2 3 4 5 6 7 8 | >>> # 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 = Length( 'goes_by' )).get() >>> print (author.name_length, author.goes_by_length) ( 14 , None ) |
It can also be registered as a transform. For example:
1 2 3 4 5 | >>> from django.db.models import CharField >>> from django.db.models.functions import Length >>> CharField.register_lookup(Length, 'length' ) >>> # Get authors whose name is longer than 7 characters >>> authors = Author.objects. filter (name__length__gt = 7 ) |
Changed in Django 1.9:
The ability to register the function as a transform was added.
Please login to continue.