This is going to be a short post. I recently had to write an abstract base class for a couple of models. The problem I encountered was that I needed to call the actual model class from a method that I defined in the abstract base class. And I was stuck.
The solution is to use the
_meta API. If you're thinking that the name
_meta is private and therefore unsafe to use. It's not private. The leading
underscore is there to avoid confusion with the the name
Meta class of the
models. So, it is safe to use.
To access the actual model class in a method in your abstract base class, you can
Here's a code example:
def save(self, *args, **kwargs):
instance = self._meta.model.objects.get(pk=self.pk)
abstract = True
I created this post because I couldn't find any resources online about this.
Hope it helps you, too.