Skip to content

Commit

Permalink
Refs #31262 -- Delegated RelatedFieldWidgetWrapper._choices handling …
Browse files Browse the repository at this point in the history
…to wrapped widget.

``ModelChoiceField`` uses ``_choices`` to avoid extra queries being
triggered and, as such, we need to pass through manipulation of this
to the underlying widget if we want to alter ``_choices`` to prevent
re-normalization.
  • Loading branch information
ngnpope committed Jul 21, 2023
1 parent e3499d0 commit 9b70683
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions django/contrib/admin/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ def choices(self):
def choices(self, value):
self.widget.choices = value

@property
def _choices(self):
return self.widget._choices

@_choices.setter
def _choices(self, value):
self.widget._choices = value

def get_related_url(self, info, action, *args):
return reverse(
"admin:%s_%s_%s" % (info + (action,)),
Expand Down
4 changes: 2 additions & 2 deletions django/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,8 @@ def choices(self):
@choices.setter
def choices(self, value):
# Setting choices on the field also sets the choices on the widget.
# Note that the property setter for the widget will re-normalize.
self._choices = self.widget.choices = normalize_field_choices(value)
# Note that we bypass the property setter to avoid re-normalizing.
self._choices = self.widget._choices = normalize_field_choices(value)

def to_python(self, value):
"""Return a string."""
Expand Down

0 comments on commit 9b70683

Please sign in to comment.