[Django] #35330: The update of related objects fails in the admin when the related model has a name in camel case.

6 views
Skip to first unread message

Django

unread,
Mar 25, 2024, 11:05:21 PM3/25/24
to django-...@googlegroups.com
#35330: The update of related objects fails in the admin when the related model has
a name in camel case.
-----------------------------------------+----------------------------
Reporter: devin13cox | Owner: devin13cox
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 1 |
-----------------------------------------+----------------------------
Related to Ticket #34789

To reproduce, take these models:

{{{
class TransitionState(models.Model):
label = models.CharField(max_length=255)

def __str__(self):
return self.label

class Transition(models.Model):
source = models.ManyToManyField(TransitionState,
related_name="transition_source")
target = models.ForeignKey(
TransitionState, on_delete=models.CASCADE,
related_name="transition_target"
)

}}}

and this admin:


{{{
class TransitionAdmin(admin.ModelAdmin):
filter_horizontal = ["source"]

site.register(TransitionState)
site.register(Transition, TransitionAdmin)
}}}

When we add a "Target", we expect the "available" source to be populated
with the new target. However, due to the camel casing of TransitionState,
we cause `data-model-ref` to check `transitionstate` against `transition
state`, and therefore does not pick up the match.

Proposed Change by @nessita as discussed in
https://github.com/django/django/pull/17897:


{{{

diff --git
a/django/contrib/admin/templates/admin/widgets/related_widget_wrapper.html
b/django/contrib/admin/templates/admin/widgets/related_widget_wrapper.html
index 8e4356a95c..99b20545af 100644
---
a/django/contrib/admin/templates/admin/widgets/related_widget_wrapper.html
+++
b/django/contrib/admin/templates/admin/widgets/related_widget_wrapper.html
@@ -1,5 +1,5 @@
{% load i18n static %}
-<div class="related-widget-wrapper" {% if not model_has_limit_choices_to
%}data-model-ref="{{ model }}"{% endif %}>
+<div class="related-widget-wrapper" {% if not model_has_limit_choices_to
%}data-model-ref="{{ model_name }}"{% endif %}>
{{ rendered_widget }}
{% block links %}
{% spaceless %}
diff --git a/django/contrib/admin/widgets.py
b/django/contrib/admin/widgets.py
index fc0cd941d1..9633ebb1a1 100644
--- a/django/contrib/admin/widgets.py
+++ b/django/contrib/admin/widgets.py
@@ -328,6 +328,7 @@ class RelatedFieldWidgetWrapper(forms.Widget):
"name": name,
"url_params": url_params,
"model": rel_opts.verbose_name,
+ "model_name": rel_opts.model_name,
"can_add_related": self.can_add_related,
"can_change_related": self.can_change_related,
"can_delete_related": self.can_delete_related,
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35330>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Reply all
Reply to author
Forward
0 new messages