How do you reference the model name in field definition in a model mixin? Ie, what would replace model_name here:
class CreatedByMixin(models.Model):
class Meta:
abstract = True
created_by = ForeignKey(
User,
verbose_name="Created by",
help_text="User that created the record",
related_name=f"{model_name}_created",
editable=False,
)
Such that the related name on this model is ‘MyModel_created’?
class MyModel(UserAuditMixin, TimeStampedModel):
class Meta:
db_table_comment = "Participants are the users that are involved in the transcript"
field1 = models.TextField()
>Solution :
You are looking for %(class)s [Django-doc]. You don’t format the string in the ForeignKey: Django automatically (re)formats the string, so you use:
class CreatedByMixin(models.Model):
class Meta:
abstract = True
created_by = ForeignKey(
User,
verbose_name='Created by',
help_text='User that created the record',
related_name='%(class)s_created',
editable=False,
)