# Imports
from django.db import models
from django.utils.translation import gettext_lazy as _
from .mixins import DisplayNameMixin
# Exports
__all__ = (
"DisplayNameModel",
)
# Models
# noinspection PyAbstractClass
[docs]class DisplayNameModel(DisplayNameMixin, models.Model):
"""Extend :py:class:`DisplayNameMixin` to also cache the display name."""
cached_choice_name = models.CharField(
_("choice name"),
blank=True,
help_text=_("The proper name of the record when used as a choice."),
max_length=128
)
cached_display_name = models.CharField(
_("display name"),
blank=True,
help_text=_("The proper name of the record."),
max_length=128
)
class Meta:
abstract = True
@property
def choice_name(self):
"""Get the cached name or the current name.
:rtype: str
"""
return self.cached_choice_name or self.get_choice_name()
@property
def display_name(self):
"""Get the cached name or the current name.
:rtype: str
"""
return self.cached_display_name or self.get_display_name()
[docs] def save(self, *args, **kwargs):
"""Cache the display name."""
self.cached_choice_name = self.get_choice_name()
self.cached_display_name = self.get_display_name()
super().save(*args, **kwargs)