2013-07-11 44 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混凝土相同性能問題在過去的幾個來源繼承實施。