[Answer]-How to present data from django-cms custom plugin in template

1👍

I think you just need:

product.InstallationStep_set.all

Since you have a ForeignKey in the InstallationStep model class, django puts the _set method into the referenced class.

You can experiment with this in the django shell. Templates will fail silently if you try doing foo.bar and there’s no bar in foo…

0👍

According to the doc, you also need to modify the copy_relations method in the model. If not, the models won’t be copied when you publish the draft and won’t see anything. I don’t understand how you manage to make it work.

In your case would be something like these:

class ProductPlugin(CMSPlugin):
    product = models.ForeignKey(ProductDescription)

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

NOTE

Not sure about the InstallationStep_set on the for because I usually use the related_name parameter in the ForeignKey fields. For example,

class InstallationStep(models.Model):
    product = models.ForeignKey(ProductDescription, related_name='installationsteps')

and your for would be:

for associated_item in oldinstance.installationsteps.all():

In any case, thanks for the question because it became my guide.

Leave a comment