db.models.functions.Cast

class Cast(expression, output_field) [source]

New in Django 1.10.

Forces the result type of expression to be the one from output_field.

Usage example:

1
2
3
4
5
6
>>> from django.db.models import FloatField
>>> from django.db.models.functions import Cast
>>> Value.objects.create(integer=4)
>>> value = Value.objects.annotate(as_float=Cast('integer', FloatField())).get()
>>> print(value.as_float)
4.0
doc_Django
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.