0

There are many SO questions about the Django errormessage Site matching query does not exist.:

In all these cases, the cause was correctly identified to be the absence of a row in the django_site table with the id specified as SITE_ID in settings.py.

Well, I want to prevent this from happening in the future. Right now, I have given a select few administrators superuser access to the Django admin. They have the power to destroy all data, if they want, and they are responsible for it. I myself, however, am responsible for server availability and want to prevent any Server 500 errors. In the current situation, my staff superusers can delete objects from the django_site thereby rendering the site inaccessible, which I find unacceptable.

How can I prevent admin superusers from rendering the complete website inaccessible?

Community
  • 1
  • 1
Jaap Joris Vens
  • 3,382
  • 2
  • 26
  • 42

1 Answers1

1

Create a custom admin class like so:

class DeleteNotAllowedModelAdmin(admin.ModelAdmin):
    # Other stuff here
    def has_delete_permission(self, request, obj=None):
        return False

If you only want to prevent deletion of say the last site and allow when there are more than one defined in the database:

class DeleteNotAllowedModelAdmin(admin.ModelAdmin):
    # Other stuff here
    def has_delete_permission(self, request, obj=None):
        if Site.objects.count() > 1:
            return True
        return False

Or something like this.

Edit: and as for how to register that custom class, look here

Allard Stijnman
  • 1,284
  • 11
  • 22