2013-07-11 27 views
1

我有一个包含各种类型的模型类的像Django的模型类:替代Django的具体继承

Content > (Audio, Video, Image) 

我要像Content.objects.filter()或该父模型内容执行查询。 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) 

在此先感谢。

回答

1

我有一个类似的场景,我已经解决了使用外部命名的Django Polymorphic,图书馆似乎无缝工作。一些主要的项目使用Django Polymorphic,包括Django-shop。

链接: https://github.com/chrisglass/django_polymorphic

不要引用我这一点,但我读过,已经提到,django_polymorphic车型没有造成标准的Django ORM混凝土相同性能问题在过去的几个来源继承实施。