For example, if I have 3 models that look like this:
class CallLog(models.Model):
lead_id = models.BigIntegerField("Lead ID")
# other fields
class EmailLog(models.Model):
lead_id = models.BigIntegerField("Lead ID")
# other fields
class TextLog(models.Model):
lead_id = models.BigIntegerField("Lead ID")
# other fields
Do I add lead_id
to each model individually or is there a way to only type it once?
CodePudding user response:
Yes, you can define an abstract base class [Django-doc]:
class LeadId(models.Model):
lead_id = models.BigIntegerField("Lead ID")
class Meta:
abstract = True
and then inherit this in the other models:
class CallLog(LeadId, models.Model):
# other fields…
class EmailLog(LeadId, models.Model):
# other fields…
class TextLog(LeadId, models.Model):
# other fields…
You can define multiple such abstract base classes, and use multiple inheritance such that models inherit from multiple of such classes.