Possible to have a on_delete=PROTECT on PageChooserBlock?

309 Views Asked by At

Let's say that I have an awesome Wagtail project. In this project I would like a Page where I can add unlimited Pages as links, the code would look like this:

pages = StreamField([
    ('link_page', blocks.PageChooserBlock(help_text='Link page')),
])

As you can see I have this StreamField with PageChooserBlock's that I can add.

My template would look like this:

{% for block in page.pages %}
    <a href='{% pageurl block.value %}'>{{ block.value.specific.title }}</a>
{% endfor %}

But what happens now if someone would delete one of the "linked" pages. They would be deleted from the pages streamfield, or at least. The streamfield would be shown, but empty.

Is there some way of adding a on_delete=PROTECT on the PageChooserBlock? like so:

pages = StreamField([
    ('link_page', blocks.PageChooserBlock(help_text='Link page', on_delete=blocks.PROTECT)),
])

If someone tries to delete the page now, they would get a violation error.

e.g. Works perfect on ForeignKey:

page = models.ForeignKey(
    'wagtailcore.Page',
    null=True,
    blank=True,
    on_delete=models.PROTECT,
    related_name='',
    help_text= 'Page',
)
1

There are 1 best solutions below

0
On BEST ANSWER

It's not possible in current versions of Wagtail - StreamField data is stored as a JSON string, which makes it difficult to identify places where a page ID is referenced within that data, and certainly not something that can be enforced at the database level.

However, there's a pull request currently in the works which will identify these cases and warn about them at the point that the page is deleted: https://github.com/wagtail/wagtail/pull/4702