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
This commit is contained in:
Oliver 2024-07-04 23:15:03 +10:00 committed by GitHub
parent 62790cddc0
commit 7f43040049
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View File

@ -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'),
),
]

View File

@ -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,
)