Django.fun

Django: 3-level base class and Meta class ignored?

I'm having the following setup in a Django 2 app:

class Base1(models.Model):
   # ... some fields here...
      class Meta:
        abstract = True
        unique_together = [
            'brand',
            'name'
        ]

class Base2(Base1):
    # ... some more fields here...
    class Meta:
        abstract = True


class Concrete(Base2):
    pass

The Concrete class doesn't seem to have the unique_together requirement that's present in Base1.

Is this expected? If it is, how can I make sure that Concrete has the constraint while keeping things DRY?

Thanks!

Answers: 1

Answered by AKX, Sept. 14, 2021, 9:47 a.m.

Try to derive Base2's Meta from Base1's.

# ...
class Base2(Base1):
    # ... some more fields here...
    class Meta(Base1.Meta):
        abstract = True
# ...

See this documentation for meta inheritance.