from django.db import models from django.utils.translation import gettext_lazy as _ # 1. Define choices globally so they can be reused class StatusColorChoices(models.TextChoices): RED = 'red', _('Red') GREEN = 'green', _('Green') BLUE = 'blue', _('Blue') YELLOW = 'yellow', _('Yellow') ORANGE = 'orange', _('Orange') PURPLE = 'purple', _('Purple') GRAY = 'gray', _('Gray') # 2. Create the Abstract Mixin class ColorPaletteMixin(models.Model): """ An abstract base class that provides color choice fields and hex code properties to any model that inherits it. """ COLOR_PALETTE = { 'red': '#D33A3A', 'green': '#1DAC43', 'blue': '#5172E1', 'yellow': '#EDC130', 'orange': '#E67E22', 'purple': '#7C5CC4', 'gray': '#374151', } color = models.CharField( max_length=20, choices=StatusColorChoices.choices, verbose_name=_('color'), default=StatusColorChoices.GRAY ) class Meta: abstract = True @property def main_color_code(self): return self.COLOR_PALETTE.get(self.color, '#000000')