I assume that all instances of ArticleBase are instances of subclasses of ArticleBase.
ArticleBase , . , .
from django.db import models
class ArticleBase(models.Model):
title = models.CharField()
author = models.CharField()
class_name = models.CharField()
def save(self, *args, **kwargs):
self.class_name = self.__class__.__name__
super(ArticleBase, self).save(*args, **kwargs)
def get_child(self):
return getattr(self, self.class_name.lower())
def get_child_class(self):
return self.get_child().__class__
def child_is(self, cls):
if isinstance(cls, basestring):
return cls.lower() == self.class_name.lower()
else:
return self.get_child_class() == cls
class Review(ArticleBase):
rating = models.IntegerField()
class News(ArticleBase):
source = models.CharField()
. , , . contenttty Contrib , , , , .
ArticleBase :
def __unicode__(self)
return self.get_child().__unicode__()
, __unicode__ __unicode__ ArticleBase (, ) . , (, ArticleBase ).
:
, , , . , , .