etc.toolbox.InheritedModel allows to override fields attributes in inherited models.
Mix in this class into target model (inherit from it) and define Fields class inside it
to be able to customize field attributes (e.g. texts) of a base-parent model.
frometc.toolboximportInheritedModelclassMyAbstractModel(models.Model):code=models.CharField('dummy',max_length=64)expired=models.BooleanField('Expired',help_text='dummy')classMeta:abstract=TrueclassSecretModel(InheritedModel,MyParentModel):# NOTE: InheritedModel must go first.time_created=models.DateTimeField('Date created',auto_now_add=True)classFields:# Defining a class with fields custom fields data.code='Secret code'# This is treated as verbose_name.expired={'help_text':'This code is expired.'}classNonSecretModel(InheritedModel,MyParentModel):code=models.CharField('dummy',max_length=128,unique=True,editable=False)classFields:code='Non-secret code'expired={'help_text':'Do not check it. Do not.'}
{% load model_field %}
{% model_field_help_text from my_model.fieldname %}
Both template tags are capable to redirect output into a template context variable using as clause. That could
be useful if you have a set of homogeneous objects (e.g. QuerySet or Page) and want to get verbose name just once:
# myapp/settings.pyfromdjango.confimportsettings# This allows users to set MYAPP_MY_MODEL in settings.py of their projects.MY_MODEL=getattr(settings,'MYAPP_MY_MODEL','myapp.MyModel')
fromenumimportEnum,unique@uniqueclassRole(ChoicesEnumMixin,Enum):# Define your Enum with mixin:# Item values could be tuples: (value, title, hint).APPLICANT=0,'Title','Hint'ADMIN=1,'Administrator'MEMBER=2classMyChoiceModel(models.Model):# Use the enum in field declaration.role=models.PositiveIntegerField(choices=get_choices(Role),default=Role.MEMBER)# Filter objects by enum values.members=MyChoiceModel.objects.filter(role=Role.MEMBER)# Access titles and hints registries# (ordered dictionaries, indexed by values):titles=Role.titleshints=Role.hints
classMyModel(models.Model):TYPE_ONE=1TYPE_TWO=2TYPES=choices_list((TYPE_ONE,'Type one title'),(TYPE_TWO,'Type two title'),)type=models.PositiveIntegerField('My type',choices=get_choices(TYPES),default=TYPE_TWO)defget_display_type(self):returnself.TYPES[self.type]