Question

I have the following models

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')))

The plugin code as below:

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

I have added slides to the plugin and published changes, however 1instance.newslide_set.all()1 returns empty list: [] 1111111111111111

Update: it creates 2 records, somehow the admin references 49, but render code gives 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 |
+----+-----------+-------+---------+------+----------------+---------+

By the way, I have django-reversion installed, not sure if it's because of this app.

Était-ce utile?

La solution

OK according to the documentation I need to copy the related items:

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()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top