I have some Django models, let's say
class Foo(models.Model): class Meta: abstract = True class Bar(Foo) pass
I would like to find all models that inherit from Foo to do the job with them. It should be easy, for example
from django.db import models from myapp.models import Foo for model in models.get_models(): if issubclass(model, Foo): do_something()
Alas, this does not work, since issubclass(Bar, Foo) reports False , possibly as a result of the internal work of the Django metaclass, which initializes the models.
Is there a way to check if the Django model is a descendant of the abstract Django model?
Please do not offer duck printing as a solution. In this case, I would really like to know if a subclass relation exists.
source share