diff options
| author | Sean Helvey <sean.helvey@gmail.com> | 2026-02-11 15:07:40 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-02-11 18:07:40 -0500 |
| commit | 380d77cccefbe185ddb3f9368d8fdeb7b7cf7108 (patch) | |
| tree | e5bc171974ffd94d492362e1f2d0d67355d7705c /django/contrib/admin/options.py | |
| parent | 97228a86d2b7d8011b97bebdfe0f126a536a3841 (diff) | |
Fixed #36921 -- Fixed KeyError in inline form for model not registered with admin.
Regression in b1ffa9a9d78b0c2c5ad6ed5a1d84e380d5cfd010.
Diffstat (limited to 'django/contrib/admin/options.py')
| -rw-r--r-- | django/contrib/admin/options.py | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 9c787d2329..b67b023bd3 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -1419,7 +1419,7 @@ class ModelAdmin(BaseModelAdmin): # Find the optgroup for the new item, if available source_model_name = request.POST.get(SOURCE_MODEL_VAR) - + source_admin = None if source_model_name: app_label, model_name = source_model_name.split(".", 1) try: @@ -1428,21 +1428,23 @@ class ModelAdmin(BaseModelAdmin): msg = _('The app "%s" could not be found.') % source_model_name self.message_user(request, msg, messages.ERROR) else: - source_admin = self.admin_site._registry[source_model] - form = source_admin.get_form(request)() - if self.opts.verbose_name_plural in form.fields: - field = form.fields[self.opts.verbose_name_plural] - for option_value, option_label in field.choices: - # Check if this is an optgroup (label is a sequence - # of choices rather than a single string value). - if isinstance(option_label, (list, tuple)): - # It's an optgroup: - # (group_name, [(value, label), ...]) - optgroup_label = option_value - for choice_value, choice_display in option_label: - if choice_display == str(obj): - popup_response["optgroup"] = str(optgroup_label) - break + source_admin = self.admin_site._registry.get(source_model) + + if source_admin: + form = source_admin.get_form(request)() + if self.opts.verbose_name_plural in form.fields: + field = form.fields[self.opts.verbose_name_plural] + for option_value, option_label in field.choices: + # Check if this is an optgroup (label is a sequence + # of choices rather than a single string value). + if isinstance(option_label, (list, tuple)): + # It's an optgroup: + # (group_name, [(value, label), ...]) + optgroup_label = option_value + for choice_value, choice_display in option_label: + if choice_display == str(obj): + popup_response["optgroup"] = str(optgroup_label) + break popup_response_data = json.dumps(popup_response) return TemplateResponse( |
