Search code examples
pythondjangodjango-modelsabstract-classabstract-methods

django models - how can i create abstract methods


In django abstract classes seem to be posibble by using:

class Meta:
    abstract = True

However I do not see how to declare abstract methods/functions within these classes that do not contain any logic like e.g.

class AbstractClass(models.Model):

    def abstractFunction():

    class Meta:
    abstract = True

The library abc repectively the notation @abstractmethod doesnt seem to applicable here, or am I wrong?


Solution

  • From what I understand, you're correct. Meta: abstract=True in Django models is there to make sure Django doesn't create database tables for the model. It predates Pythons ABC, which might explain the naming/functional confusion.

    Inheriting from both ABC and django.db.models.Model raises metaclass exception

    However, a quick search gave me this. Don't know if it breaks anything. https://gist.github.com/gavinwahl/7778717

    I do like how ABC raises an exception at class instantiation, so it would be pretty neat. Let me know if you find it useful.