question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for PROTECT on foreign keys

See original GitHub issue

Are there plans to support on_delete=django.db.models.PROTECT in the admin? Right now ProtectedError is not caught.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:6
  • Comments:17 (13 by maintainers)

github_iconTop GitHub Comments

4reactions
spapascommented, Jan 22, 2020

Unfortunately the solution @pySilver posted works only for pages (that have the before_delete_page hook). It isn’t applicable when deleting images, documents or whatever else that is used needs to be deleted.

What I propose instead is to use the following middleware:

class HandleProtectionErrorMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def process_exception(self, request, exception):
        from django.db.models.deletion import ProtectedError
        from django.contrib import messages
        from django.http import HttpResponseRedirect

        if isinstance(exception, ProtectedError):
            messages.error(
                request,
                "The object you are trying to delete is used somewhere. Please remove any usages and try again!.",
            )
            return HttpResponseRedirect(request.path)

        return None

    def __call__(self, request):
        response = self.get_response(request)
        return response

Just add it to the end of your MIDDLEWARE list. If a ProtectedError is thrown it will catch it and add a proper message to the view gracefully handling the excception. I think that this middleware needs to be a part of the wagtail default installation or at least mentioned somewhere in the docs so that people can use it if they want it.

2reactions
pySilvercommented, Jan 22, 2020

@spapas I just mean that as workaround this middleware is fine (which is great since there is nothing else available right now), but as part of wagtail it should be done on a lower level.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Foreign Key Constraint | CockroachDB Docs
The `FOREIGN KEY` constraint specifies a column can contain only values exactly matching existing values from the column it references.
Read more >
Primary and Foreign Key Constraints - SQL Server
Primary keys and foreign keys are two types of constraints that can be used to enforce data integrity in SQL Server tables.
Read more >
SQLite Foreign Key Support
SQL foreign key constraints are used to enforce "exists" relationships between tables. For example, consider a database schema created using the ...
Read more >
Foreign keys | Cloud Spanner
Foreign keys cannot be created on columns with the allow_commit_timestamp=true option. Array columns are not supported. JSON columns are not supported. A ...
Read more >
Foreign keys and associations - GitLab Docs
Add a foreign key here on column posts.user_id . This ensures that data consistency is enforced on database level. Foreign keys also mean...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found