5

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?

Ilir
  • 430
  • 2
  • 7
  • 19

1 Answers1

6

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.

Johan Schiff
  • 661
  • 4
  • 13
  • thx @Johan Schiff, this is just the missing part of the puzzle. https://gist.github.com/gavinwahl/7778717 shows pretty well the use of abc in django. – Ilir Mar 30 '20 at 06:56