aemdy
aemdy

Reputation: 3802

Django abstract models with M2M fields

Let's suppose I have the following:

class Base(Model):
    m2m_1 = ManyToManyField("SomeModel1")
    m2m_2 = ManyToManyField("SomeModel2")

    class Meta:
        abstract = True

class A(Base):
    def __init__(self):
        super(A, self).__init__()
    pass

class B(Base):
    def __init__(self):
        super(B, self).__init__()
    pass

However, I cannot do that because it requires related name for M2M field. However, that does not help as the model is abstract and django tries to create the same related name for both A and B models.

Any ideas how to specify related names for each model separately or even do not use them at all?

Upvotes: 1

Views: 2034

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239430

The answer is right in the docs for abstract classes (under section entitled "Be careful with related_name"):

m2m = models.ManyToManyField(OtherModel, related_name="%(app_label)s_%(class)s_related")

Upvotes: 9

Related Questions