2011-03-26 64 views
3

我是FeinCMS的新手,我正尝试创建自己的内容类型。这使用我创建的另一个自定义内容类型。FeinCMS管理员内嵌

在下面的代码中,“CollapsiblePanel”未显示在管理员中,因为我只希望您能够从ContentBox部分创建“CollapsiblePanels”。

您也可以为每个ContentBox创建多个CollapsiblePanel。我无法搞清楚如何把这个在一起,使管理员可以让你添加ContentBox

class CollapsiblePanel(models.Model): 
    title = models.CharField(max_length=255) 
    content = models.TextField() 

    def render(self, **kwargs): 
     return render_to_string('collapsiblepanel.django.html', { 
      'media': self, 
      'title': mark_safe(self.title), 
      'text': mark_safe(self.content), 
     }) 

class ContentBoxMedia(RichTextContent): 
    title = models.CharField(_('title'), max_length=200, blank=True) 
    collapsible = models.BooleanField() 
    collapsiblePanels = models.ForeignKey(CollapsiblePanel) 

    class Meta: 
     abstract = True 
     verbose_name = 'Content Box' 
     verbose_name_plural = 'Content Box' 

    def render(self, **kwargs): 
     return render_to_string('contentbox.django.html', { 
      'media': self, 
      'title': mark_safe(self.title), 
      'text': mark_safe(self.text), 
     }) 
+0

看来这个代码有两个问题。首先,CollapsiblePanel的ForeignKey可能是错误的 - 它应该是从你的描述中判断的另一种方式。其次,不能使用内联内联进行编辑 - 股票Django不允许这样做,因此FeinCMS也没有。 – 2011-03-26 20:18:17

+0

哈哈;正如我刚才在我的回答中解释的那样... Hi Matthias :) – DrMeers 2011-03-26 20:37:37

回答

2

内CollapsiblePanels如果您应该能够每ContentBoxMedia有多个CollapsiblePanel S,你的关系是建立错误的方式 - ForeignKey应改为CollapsiblePanel

但是,看起来您所要做的是自动处理CollapsiblePanel“inline”吗?这不会开箱即用,因为FeinCMS将所有内容类型处理为内联(所以ContentBoxMedia对象已经作为父对象的内联处理),并且Django不支持nested inlines

我怀疑任何黑客提供这种功能将是非常复杂的;您可以尝试在ContentBoxMedia模板中呈现您自己的表单集,但是您需要破解ItemEditor.change_view方法来处理数据,这很不容易。或者,你可以通过采用Ajax方法避免这种情况,但这只能在保存的ContentBoxMedia对象中使用,而不是新的。

或者,您可以尝试直接向管理员注册ContentBoxMedia,以便您可以使用CollapsiblePanel内联,但这需要让主FeinCMS父管理页面单独编辑这些内联。如果你想探索这个,你需要使用Base.content_type_for并注册结果模型AdminSite(当然,明确注册一个内联)。

+0

谢谢你的回复。这是我想出的,但现在我有一个渲染问题。 [Pastebin Code](http://pastebin.com/wC58qgy0)呈现问题是feincms不会呈现我从下拉列表中选择的内容。我不确定我的渲染函数应该是什么样子。 – Jeremy 2011-03-27 22:55:32

+0

你能提供一个简单的例子,说明我会如何做到这一点? [Base.content_type_for](http://www.feinheit.ch/media/labs/feincms/contenttypes.html#obtaining-a-concrete-content-type-model) – Jeremy 2011-03-28 00:04:27

+0

这是一个链接到文档,它有一个简单的用法示例。 'x = MyBaseModel.content_type_for(ContentBoxMedia)'然后'admin.site.register(x)'或类似的。但是不要浪费时间,除非你确信你明白它的含义。您更新的示例模型似乎不是抽象的,这意味着无论如何它都不能用作FeinCMS内容类型。 – DrMeers 2011-03-28 04:35:50