extensions-website/extensions/fields.py
Oleg Komarov dfaaccee4a models: simplify VersionStringField
this field is really a CharField with appropriate validation:
it is always presented and processed as a string

previous implementation was rather inefficient and
VersionStringField.from_db_value was showing up in API profiling:
validation was happening every time a Version object was initialized
by ORM

migrations have to be changed retroactively to remove parameters that
become invalid, but this doesn't affect the data
2024-07-12 22:52:17 +02:00

17 lines
441 B
Python

from django.core.exceptions import ValidationError
from django.db.models import CharField
from semantic_version import Version
def validate_version_string(version_string):
try:
Version.parse(version_string)
except ValueError as e:
raise ValidationError(e)
class VersionStringField(CharField):
description = "A field to store serializable semantic versions"
default_validators = [validate_version_string]