[Fixed]-Django CMS page extension data duplication

1๐Ÿ‘

โœ…

cms.Page objects exist in published and draft form, and the draft (along with all the objects associated with it) is copied to the other version in the Publish operation.

Your MyAppExtension.objects have ManyToManyField attributes. These will need to be copied too, otherwise the published version will fail โ€“ as you have discovered โ€“ to get back to these objects.

Handling relations in the documentation for Page extensions explains what to do and gives an example โ€“ in brief, provide a copy_relations() method on the Page extension, that copies them to the new instance.

Without this method youโ€™ll find that all copy operations on the page, not just publishing, fail to copy the objects.

๐Ÿ‘คDaniele Procida

0๐Ÿ‘

Maybe you can use something like this:

class PageDataExtension(PageExtension):

    category_page = models.ManyToManyField(category, blank=True)

    def copy_relations(self, oldinstance, language):
        self.category_page.clear()

        for pcategory in oldinstance.category_page.all():
            self.category_page.add(pcategory)
๐Ÿ‘คdarthwiin

Leave a comment