diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index abba0c549..0e5536233 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,7 +10,7 @@ repos: rev: 24.8.0 hooks: - id: black - language_version: python3.8 + language_version: python3.9 - repo: https://github.com/pycqa/flake8 rev: 7.1.1 diff --git a/simple_history/admin.py b/simple_history/admin.py index 6a55e2e1c..1da2016fd 100644 --- a/simple_history/admin.py +++ b/simple_history/admin.py @@ -5,7 +5,7 @@ from django.conf import settings from django.contrib import admin from django.contrib.admin import helpers -from django.contrib.admin.utils import unquote +from django.contrib.admin.utils import flatten_fieldsets, unquote from django.contrib.auth import get_permission_codename, get_user_model from django.core.exceptions import PermissionDenied from django.db.models import QuerySet @@ -240,11 +240,18 @@ def history_form_view(self, request, object_id, version_id, extra_context=None): else: form = form_class(instance=obj) + fieldsets = self.get_fieldsets(request, obj) + + if object_id and not self.has_change_permission(request, obj): + readonly_fields = flatten_fieldsets(fieldsets) + else: + readonly_fields = self.get_readonly_fields(request, obj) + admin_form = helpers.AdminForm( form, - self.get_fieldsets(request, obj), + fieldsets, self.prepopulated_fields, - self.get_readonly_fields(request, obj), + readonly_fields, model_admin=self, )