Fixed #33155 -- Made ModelChoiceIteratorValue instances hashable.
This commit is contained in:
parent
903aaa35e5
commit
7b8beeee3d
1
AUTHORS
1
AUTHORS
|
@ -54,6 +54,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
Alexey Boriskin <alex@boriskin.me>
|
Alexey Boriskin <alex@boriskin.me>
|
||||||
Alexey Tsivunin <most-208@yandex.ru>
|
Alexey Tsivunin <most-208@yandex.ru>
|
||||||
Ali Vakilzade <ali@vakilzade.com>
|
Ali Vakilzade <ali@vakilzade.com>
|
||||||
|
Aljaž Košir <aljazkosir5@gmail.com>
|
||||||
Aljosa Mohorovic <aljosa.mohorovic@gmail.com>
|
Aljosa Mohorovic <aljosa.mohorovic@gmail.com>
|
||||||
Amit Chakradeo <https://amit.chakradeo.net/>
|
Amit Chakradeo <https://amit.chakradeo.net/>
|
||||||
Amit Ramon <amit.ramon@gmail.com>
|
Amit Ramon <amit.ramon@gmail.com>
|
||||||
|
|
|
@ -1166,6 +1166,9 @@ class ModelChoiceIteratorValue:
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.value)
|
return str(self.value)
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return hash(self.value)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, ModelChoiceIteratorValue):
|
if isinstance(other, ModelChoiceIteratorValue):
|
||||||
other = other.value
|
other = other.value
|
||||||
|
|
|
@ -2,7 +2,7 @@ import datetime
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.forms.models import ModelChoiceIterator
|
from django.forms.models import ModelChoiceIterator, ModelChoiceIteratorValue
|
||||||
from django.forms.widgets import CheckboxSelectMultiple
|
from django.forms.widgets import CheckboxSelectMultiple
|
||||||
from django.template import Context, Template
|
from django.template import Context, Template
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
@ -341,6 +341,12 @@ class ModelChoiceFieldTests(TestCase):
|
||||||
</div>""" % (self.c1.pk, self.c2.pk, self.c3.pk),
|
</div>""" % (self.c1.pk, self.c2.pk, self.c3.pk),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_choice_value_hash(self):
|
||||||
|
value_1 = ModelChoiceIteratorValue(self.c1.pk, self.c1)
|
||||||
|
value_2 = ModelChoiceIteratorValue(self.c2.pk, self.c2)
|
||||||
|
self.assertEqual(hash(value_1), hash(ModelChoiceIteratorValue(self.c1.pk, None)))
|
||||||
|
self.assertNotEqual(hash(value_1), hash(value_2))
|
||||||
|
|
||||||
def test_choices_not_fetched_when_not_rendering(self):
|
def test_choices_not_fetched_when_not_rendering(self):
|
||||||
with self.assertNumQueries(1):
|
with self.assertNumQueries(1):
|
||||||
field = forms.ModelChoiceField(Category.objects.order_by('-name'))
|
field = forms.ModelChoiceField(Category.objects.order_by('-name'))
|
||||||
|
|
Loading…
Reference in New Issue