我有一个 Django 模型类,其中包含各种类型的模型类,例如:
Content > (Audio, Video, Image)
我想对此父模型执行查询,例如 Content.objects.filter() 或 Content.objects.recent()。我怎么会这样?目前我正在使用 django 的具体模型继承,我想通过使用父类的连接对数据库性能施加了很多偷听。我不能使用抽象类,因为那不允许我对父类执行查询。这是我的模型定义:
class BaseContent(models.Model):
"""
Concrete base model to encompass all the local and social content.
We need a single queryset for all the content on the app.
"""
title = models.CharField(_('title'), max_length=255, default='')
description = models.TextField(_('description'), default='', blank=True)
publisher = models.ForeignKey(settings.AUTH_USER_MODEL)
allow_comments = models.BooleanField(default=False)
is_public = models.BooleanField(default=True)
created = AutoCreatedField(_('created'))
objects = BaseContentManager()
class Video(BaseContent):
ACTIVITY_ACTION = 'posted a video'
UPLOAD_TO = 'video_files/%Y/%m/%d'
PREVIEW_UPLOAD_TO = 'video_frames/%Y/%m/%d'
video_file = models.FileField(_('video file'), upload_to=UPLOAD_TO)
preview_frame = models.ImageField(
_('Preview image'), upload_to=PREVIEW_UPLOAD_TO, blank=True,
null=True)
category = models.ForeignKey(VideoCategory, blank=True, null=True)
num_plays = models.PositiveIntegerField(blank=True, default=0)
num_downloads = models.PositiveIntegerField(blank=True, default=0)
提前致谢。