django models - how can i create abstract methods
Asked Answered
C

1

7

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?

Caddaric answered 29/3, 2020 at 21:19 Comment(0)
A
8

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.

Afterburner answered 29/3, 2020 at 21:35 Comment(1)
thx @Johan Schiff, this is just the missing part of the puzzle. gist.github.com/gavinwahl/7778717 shows pretty well the use of abc in django.Caddaric

© 2022 - 2024 — McMap. All rights reserved.