summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2019-11-17 15:41:23 -0800
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-12-23 10:34:50 +0100
commit67ea35df52f2e29bafca8881e4f356934061644e (patch)
tree42dfda9db4b6d8db3792ee6939c06019fc55477a /django/forms
parent5da85ea73724d75e609c5ee4316e7e5be8f17810 (diff)
Fixed #30998 -- Added ModelChoiceIteratorValue to pass the model instance to ChoiceWidget.create_option().
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/models.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index 0684199db5..d05931ab6e 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -1126,6 +1126,20 @@ class InlineForeignKeyField(Field):
return False
+class ModelChoiceIteratorValue:
+ def __init__(self, value, instance):
+ self.value = value
+ self.instance = instance
+
+ def __str__(self):
+ return str(self.value)
+
+ def __eq__(self, other):
+ if isinstance(other, ModelChoiceIteratorValue):
+ other = other.value
+ return self.value == other
+
+
class ModelChoiceIterator:
def __init__(self, field):
self.field = field
@@ -1151,7 +1165,10 @@ class ModelChoiceIterator:
return self.field.empty_label is not None or self.queryset.exists()
def choice(self, obj):
- return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
+ return (
+ ModelChoiceIteratorValue(self.field.prepare_value(obj), obj),
+ self.field.label_from_instance(obj),
+ )
class ModelChoiceField(ChoiceField):