|
7 | 7 | import math |
8 | 8 |
|
9 | 9 | from django.db.models.functions import ( |
10 | | - Chr, Cot, Degrees, Left, Log, Ord, Pi, Radians, Right, StrIndex, Substr, |
| 10 | + Cast, Chr, Cot, Degrees, Left, Log, Ord, Pi, Radians, Right, StrIndex, |
| 11 | + Substr, |
11 | 12 | ) |
12 | 13 |
|
13 | 14 |
|
| 15 | +def cast(self, compiler, connection, **extra_context): |
| 16 | + # Account for a field's max_length using SUBSTR. |
| 17 | + max_length = getattr(self.output_field, 'max_length', None) |
| 18 | + if max_length is not None: |
| 19 | + template = 'SUBSTR(' + self.template + ', 0, %s)' % max_length |
| 20 | + else: |
| 21 | + template = self.template |
| 22 | + return self.as_sql(compiler, connection, template=template, **extra_context) |
| 23 | + |
| 24 | + |
14 | 25 | def chr_(self, compiler, connection, **extra_context): |
15 | 26 | return self.as_sql(compiler, connection, template='CODE_POINTS_TO_STRING([%(expressions)s])', **extra_context) |
16 | 27 |
|
@@ -64,6 +75,7 @@ def substr(self, compiler, connection, **extra_context): |
64 | 75 |
|
65 | 76 |
|
66 | 77 | def register_functions(): |
| 78 | + Cast.as_spanner = cast |
67 | 79 | Chr.as_spanner = chr_ |
68 | 80 | Cot.as_spanner = cot |
69 | 81 | Degrees.as_spanner = degrees |
|
0 commit comments