Fix: force casting for postgres

This commit is contained in:
shamoon 2024-09-29 09:25:40 -07:00
parent dba800b5ed
commit a9913bc79d
2 changed files with 24 additions and 7 deletions

View File

@ -1,5 +1,6 @@
# Generated by Django 5.1.1 on 2024-09-29 05:16 # Generated by Django 5.1.1 on 2024-09-29 16:13
import django.db.models.functions.comparison
import django.db.models.functions.text import django.db.models.functions.text
from django.db import migrations from django.db import migrations
from django.db import models from django.db import models
@ -18,13 +19,22 @@ class Migration(migrations.Migration):
db_persist=True, db_persist=True,
expression=models.Case( expression=models.Case(
models.When( models.When(
then=django.db.models.functions.text.Substr( then=django.db.models.functions.comparison.Cast(
"value_monetary", django.db.models.functions.text.Substr("value_monetary", 1),
1, output_field=models.DecimalField(
decimal_places=2,
max_digits=125,
),
), ),
value_monetary__regex="^\\d+", value_monetary__regex="^\\d+",
), ),
default=django.db.models.functions.text.Substr("value_monetary", 4), default=django.db.models.functions.comparison.Cast(
django.db.models.functions.text.Substr("value_monetary", 4),
output_field=models.DecimalField(
decimal_places=2,
max_digits=125,
),
),
output_field=models.DecimalField(decimal_places=2, max_digits=125), output_field=models.DecimalField(decimal_places=2, max_digits=125),
), ),
output_field=models.DecimalField(decimal_places=2, max_digits=125), output_field=models.DecimalField(decimal_places=2, max_digits=125),

View File

@ -23,6 +23,7 @@ if settings.AUDIT_LOG_ENABLED:
from auditlog.registry import auditlog from auditlog.registry import auditlog
from django.db.models import Case from django.db.models import Case
from django.db.models.functions import Cast
from django.db.models.functions import Substr from django.db.models.functions import Substr
from django_softdelete.models import SoftDeleteModel from django_softdelete.models import SoftDeleteModel
@ -929,10 +930,16 @@ class CustomFieldInstance(models.Model):
# If the value starts with a number and no currency symbol, use the whole string # If the value starts with a number and no currency symbol, use the whole string
models.When( models.When(
value_monetary__regex=r"^\d+", value_monetary__regex=r"^\d+",
then=Substr("value_monetary", 1), then=Cast(
Substr("value_monetary", 1),
output_field=models.DecimalField(decimal_places=2, max_digits=125),
),
), ),
# If the value starts with a 3-char currency symbol, use the rest of the string # If the value starts with a 3-char currency symbol, use the rest of the string
default=Substr("value_monetary", 4), default=Cast(
Substr("value_monetary", 4),
output_field=models.DecimalField(decimal_places=2, max_digits=125),
),
output_field=models.DecimalField(decimal_places=2, max_digits=125), output_field=models.DecimalField(decimal_places=2, max_digits=125),
), ),
output_field=models.DecimalField(decimal_places=2, max_digits=125), output_field=models.DecimalField(decimal_places=2, max_digits=125),