From eb150b7eeb0edf2c623c3e9f0b10eed7ef6cfcf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marduk=20Bola=C3=B1os?= Date: Thu, 18 Jul 2024 09:47:51 +0200 Subject: [PATCH] Add dependent-field support to formsets from PR #231 --- django_select2/static/django_select2/django_select2.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/django_select2/static/django_select2/django_select2.js b/django_select2/static/django_select2/django_select2.js index 01db20b5..bcc3829e 100644 --- a/django_select2/static/django_select2/django_select2.js +++ b/django_select2/static/django_select2/django_select2.js @@ -28,7 +28,13 @@ if (dependentFields) { dependentFields = dependentFields.trim().split(/\s+/) $.each(dependentFields, function (i, dependentField) { - result[dependentField] = $('[name=' + dependentField + ']', $element.closest('form')).val() + var formValue = $('[name=' + dependentField + ']', $element.closest('form')).val(); + // This is for inlines, I checked this for a specific case + if (formValue === null || formValue === undefined) { + var newFieldName = $element[0].name.split('-', 2).join('-') + '-' + dependentField; + formValue = $('[name=' + newFieldName + ']', $element.closest('.form-row')).val(); + } + result[dependentField] = formValue; }) }