0

我有以下型号

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,不确定是不是因为这个应用程序。

4

1 回答 1

1

好的,根据文档我需要复制相关项目:

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()
于 2013-11-04T23:41:17.450 回答