From 7f43040049bc0f830d5694bca410b188dd6a71d8 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 4 Jul 2024 23:15:03 +1000 Subject: [PATCH] Fix 'symbol' field for CustomUnit (#7557) * Fix 'symbol' field for CustomUnit - Do not require 'symbol' to be unique * Run check as part of validate_unique --- .../migrations/0027_alter_customunit_symbol.py | 18 ++++++++++++++++++ src/backend/InvenTree/common/models.py | 15 +++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/backend/InvenTree/common/migrations/0027_alter_customunit_symbol.py diff --git a/src/backend/InvenTree/common/migrations/0027_alter_customunit_symbol.py b/src/backend/InvenTree/common/migrations/0027_alter_customunit_symbol.py new file mode 100644 index 0000000000..a897773201 --- /dev/null +++ b/src/backend/InvenTree/common/migrations/0027_alter_customunit_symbol.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.12 on 2024-07-04 10:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0026_auto_20240608_1238'), + ] + + operations = [ + migrations.AlterField( + model_name='customunit', + name='symbol', + field=models.CharField(blank=True, help_text='Optional unit symbol', max_length=10, verbose_name='Symbol'), + ), + ] diff --git a/src/backend/InvenTree/common/models.py b/src/backend/InvenTree/common/models.py index 3e49bbef71..0309caf442 100644 --- a/src/backend/InvenTree/common/models.py +++ b/src/backend/InvenTree/common/models.py @@ -14,7 +14,7 @@ from datetime import timedelta, timezone from enum import Enum from io import BytesIO from secrets import compare_digest -from typing import Any, Callable, TypedDict, Union +from typing import Any, Callable, Collection, TypedDict, Union from django.apps import apps from django.conf import settings as django_settings @@ -3042,6 +3042,18 @@ class CustomUnit(models.Model): return fmt + def validate_unique(self, exclude=None) -> None: + """Ensure that the custom unit is unique.""" + super().validate_unique(exclude) + + if self.symbol: + if ( + CustomUnit.objects.filter(symbol=self.symbol) + .exclude(pk=self.pk) + .exists() + ): + raise ValidationError({'symbol': _('Unit symbol must be unique')}) + def clean(self): """Validate that the provided custom unit is indeed valid.""" super().clean() @@ -3083,7 +3095,6 @@ class CustomUnit(models.Model): max_length=10, verbose_name=_('Symbol'), help_text=_('Optional unit symbol'), - unique=True, blank=True, )