Mažas Mažas - 4 months ago 21
Python Question

django CMS page extension data duplication

I have a django CMS page extension:

class MyAppExtension(PageExtension):
show_in_catalog = models.BooleanField()
filters_to_show = models.ManyToManyField(Attribute)

extension_pool.register(MyAppExtension)


Views:

class MyListView(FilterView):
.....

def get_filterset_kwargs(self, filterset_class):
extension = MyAppExtension.objects.get(
extended_object=self.request.current_page)
attributes = extension.filters_to_show.all()


Now in every page I should get the attributes list. But in Live mode I get an empty list. When I switch to Draft mode, I get the attributes list as expected, like it should be.

After some messing, I discovered that
MyAppExtension.objects.all()
is duplicated for each page, with an empty
filters_to_show
list in each case.

Also, in Live mode when I call
MyAppExtension.objects.get(extended_object=self.request.current_page)
it has a different
pk
from when I call that in Draft mode. So my questions:


  • What is happening?

  • How can I get the correct object in Live mode?

  • Why does every extension have a duplicate?



live to draft

Answer

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.

Comments