django cms插件实例related_set返回空列表

时间:2013-11-04 22:58:48

标签: plugins django-cms

我有以下型号

class NewSlide(models.Model):
    slider = models.ForeignKey('NewSliderPlugin')
    title = models.CharField(max_length=255)
    content = models.TextField(max_length=80, null=True)
    link = models.CharField(max_length=255)
    image = models.ImageField(upload_to='slides', null=True)
    visible = models.BooleanField(default=False)

    def __unicode__(self):  # Python 3: def __str__(self):
        return self.title

class NewSliderPlugin(CMSPlugin):
    title = models.CharField(max_length=255)
    template = models.CharField(max_length=255, choices=(('slider.html','Top Level Slider'), ('slider2.html','Featured Slider')))

插件代码如下:

class NewSlideInline(admin.StackedInline):
    model = NewSlide
    extra = 1

class NewCMSSliderPlugin(CMSPluginBase):
    model = NewSliderPlugin
    name = "NewSlider"
    render_template = "slider.html"
    inlines = [NewSlideInline]
    def render(self, context, instance, placeholder):

        self.render_template = instance.template
        print instance.title
        print instance.newslide_set.all(), 1111111111111111
        context.update({
            'slider': instance,
            'object': instance,
            'placeholder': placeholder
        })
        return context

我已将幻灯片添加到插件并发布了更改,但是1instance.newslide_set.all()1返回空列表:[] 1111111111111111

更新: 它创建2条记录,不知何故管理员引用49,但渲染代码给出了63

mysql> select * from cmsplugin_newsliderplugin;
+------------------+-----------+-------------+
| cmsplugin_ptr_id | title     | template    |
+------------------+-----------+-------------+
|               49 | slide     | slider.html |
|               63 | slide     | slider.html |
+------------------+-----------+-------------+

mysql> select * from slider_newslide;
+----+-----------+-------+---------+------+----------------+---------+
| id | slider_id | title | content | link | image          | visible |
+----+-----------+-------+---------+------+----------------+---------+
|  6 |        49 | ttttt | testt   | test | slides/287.jpg |       0 |
+----+-----------+-------+---------+------+----------------+---------+

顺便说一句,我安装了django-reversion,不确定是不是因为这个应用程序。

1 个答案:

答案 0 :(得分:1)

根据我需要复制相关项目的documentation确定:

class NewSliderPlugin(CMSPlugin):
    title = models.CharField(max_length=255)
    template = models.CharField(max_length=255, choices=(('slider.html','Top Level Slider'), ('slider2.html','Featured Slider')))

    def copy_relations(self, oldinstance):
        for slide in oldinstance.newslide_set.all():
            # instance.pk = None; instance.pk.save() is the slightly odd but
            # standard Django way of copying a saved model instance
            slide.pk = None
            slide.slider = self
            slide.save()