From 707feea1e0adae1d7c9b897770b1e5c87f6c0610 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 14 Oct 2025 22:30:49 +0800 Subject: [PATCH] Fix inline add permission check in tabular template and JavaScript - Template: Only set id='empty' when has_add_permission is True - JavaScript: Check template existence before cloning (click handler) - JavaScript: Check has_add_permission before showing add button - Align with Django's original tabular.html template behavior - Prevents users from adding inline items when has_add_permission returns False Fixes issue where add button was clickable even without permission, causing JavaScript errors when attempting to clone non-existent template. --- src/unfold/static/admin/js/inlines.js | 10 +++++++++- .../unfold/helpers/edit_inline/tabular_row.html | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/unfold/static/admin/js/inlines.js b/src/unfold/static/admin/js/inlines.js index 2f3dbb88c..45da8a9f5 100644 --- a/src/unfold/static/admin/js/inlines.js +++ b/src/unfold/static/admin/js/inlines.js @@ -91,6 +91,10 @@ const addInlineClickHandler = function (e) { e.preventDefault(); const template = $("#" + options.prefix + "-empty"); + // Check if template exists (has_add_permission check) + if (template.length === 0) { + return; + } const row = template.clone(true); row .removeClass(options.emptyCssClass) @@ -284,8 +288,12 @@ // Show the add button if allowed to add more items. // Note that max_num = None translates to a blank string. + // Check if template exists (has_add_permission check) + const template = $("#" + options.prefix + "-empty"); + const hasAddPermission = template.length > 0; const showAddButton = - maxForms.val() === "" || maxForms.val() - totalForms.val() > 0; + hasAddPermission && + (maxForms.val() === "" || maxForms.val() - totalForms.val() > 0); if ($this.length && showAddButton) { addButton.parent().show(); } else { diff --git a/src/unfold/templates/unfold/helpers/edit_inline/tabular_row.html b/src/unfold/templates/unfold/helpers/edit_inline/tabular_row.html index 1d9547c8e..c618567d8 100644 --- a/src/unfold/templates/unfold/helpers/edit_inline/tabular_row.html +++ b/src/unfold/templates/unfold/helpers/edit_inline/tabular_row.html @@ -1,4 +1,4 @@ - + {% spaceless %} {% for fieldset in inline_admin_form %} {% for line in fieldset %}