[Solved]-Django – how to determine if model class is abstract

16👍

You can instantiate MyModel and then check ._meta.abstract.

So in code:

m = MyModel()
print m._meta.abstract
👤sheats

3👍

I’d like to point out that you don’t need to instantiate a model to check if it’s abstract – Django models inherit an actual metaclass that adds _meta on class instantiation.

So, similarly to @sheats’s code, try

from django.db.models import Model
class MyModel(Model):
  pass
print MyModel._meta.abstract

Or, for a positive example

from django.db.models import Model
class MyModel(Model):
  class Meta(object):
    abstract = True
print MyModel._meta.abstract

Of course, this also works for built-in models and anything inheriting from Django’s Model.

from django.contrib.auth.models import User
print User._meta.abstract

Leave a comment