您可以添加一个额外的 BooleanField is_ghost
,并覆盖原始模型和代理模型上的默认管理器:
class PostManager(models.Manager):
def get_querset(self):
return super(PostManager, self).get_queryset().filter(is_ghost=False)
class Post(models.Model):
...
is_ghost = models.BooleanField(blank=True, default=False)
objects = PostManager()
class GhostPostManager(models.Manager):
def get_queryset(self):
return super(GhostPostManager, self).get_queryset().filter(is_ghost=True)
class GhostPost(Post):
objects = GhostPostManager()
Meta:
proxy = True
这会将所有内容保存在一个表中,但管理器和查询集将像两个单独的模型/表一样工作。
另一种解决方案是对两个模型都使用抽象基类。如果您不更改原始Post
模型的类名,并从 eg 继承AbstractPost
,我认为这不会导致任何问题。