2014-04-19 109 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 
    ] 
相关问题