2014-04-19 36 views
0

我最近第一次使用django cms,並且已經創建了一個圖庫插件來上傳圖片。django cms和自定義插件模型之間有什麼關係?

這是一個非常簡單的插件,使用ImageGalleryPlugin模型,從CMSPluginBase繼承,然後一個Image模型具有ForeignKey的畫廊。

使用佔位符將圖庫插件附加到頁面上,然後查看圖庫中的圖像我已創建apphook以將插件模板鏈接到類似於;

def detail(request, page_id=None, gallery_id=None): 
    """ 
    View to display all the images in a chosen gallery 
    and also provide links to the other galleries from the page 
    """ 
    gallery = get_object_or_404(ImageGalleryPlugin, pk=gallery_id) 
    # Then get all the other galleries to allow linking to those 
    # from within a gallery 
    more_galleries = ImageGalleryPlugin.objects.all().exclude(pk=gallery.id) 
    images = gallery.images_set.all() 

    context = RequestContext(request.context, { 
     'images': images, 
     'gallery': gallery, 
     'more_galleries': more_galleries 
    }) 
    return render_to_template('gallery-page.html', context) 

現在我有這個方法的問題是,當你在CMS發佈一個網頁它複製所有ImageGalleryPlugin對象在該網頁上,所以當我查看圖像,我已經得到了兩倍多鏈接到其他畫廊,因爲查詢收集重複的對象。

我無法正確理解文檔中的這種行爲,但我認爲CMS會保留您創建的原始對象,然後將重複項創建爲「實時」版畫廊以向用戶顯示。

在CMS中發生這種情況,我的ImageGalleryPlugin對象的ID在哪裏存儲,以便我只能在此視圖中收集正確的對象,而不是收集所有對象?

回答

0

我終於解決了我自己的問題。

我的CMSPlugin對象與Page之間的關聯是myobj.placeholder.page所以我的過濾解決方案是;

# Get the ImageGalleryPlugin object 
    gallery = get_object_or_404(ImageGalleryPlugin, pk=gallery_id) 

    # Get all other ImageGalleryPlugin objects 
    galleries = ImageGalleryPlugin.objects.all().exclude(pk=gallery.id) 

    # Get all the images in the chosen gallery 
    images = gallery.image_field.images 

    # Filter the list of other galleries so that we don't get galleries 
    # attached to other pages. 
    more_galleries = [ 
     g for g in galleries if unicode(g.placeholder.page.id) == page_id 
    ] 
相關問題