From 0a49276065575867fbbf279f138b399c535e3b6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alja=C5=BE=20Ko=C5=A1ir?= Date: Thu, 30 Sep 2021 12:07:50 +0200 Subject: [PATCH] [4.0.x] Fixed #33155 -- Made ModelChoiceIteratorValue instances hashable. Backport of 7b8beeee3d45cafd7bec7ff3ee0e4371e142c36d from main --- AUTHORS | 1 + django/forms/models.py | 3 +++ tests/model_forms/test_modelchoicefield.py | 8 +++++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 214aca2480..146b0be3a3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -54,6 +54,7 @@ answer newbie questions, and generally made Django that much better: Alexey Boriskin Alexey Tsivunin Ali Vakilzade + Aljaž Košir Aljosa Mohorovic Amit Chakradeo Amit Ramon diff --git a/django/forms/models.py b/django/forms/models.py index 5dcf923c12..7effb202e3 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -1166,6 +1166,9 @@ class ModelChoiceIteratorValue: def __str__(self): return str(self.value) + def __hash__(self): + return hash(self.value) + def __eq__(self, other): if isinstance(other, ModelChoiceIteratorValue): other = other.value diff --git a/tests/model_forms/test_modelchoicefield.py b/tests/model_forms/test_modelchoicefield.py index 2a0c05d803..30d1a05821 100644 --- a/tests/model_forms/test_modelchoicefield.py +++ b/tests/model_forms/test_modelchoicefield.py @@ -2,7 +2,7 @@ import datetime from django import forms 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.template import Context, Template from django.test import TestCase @@ -341,6 +341,12 @@ class ModelChoiceFieldTests(TestCase): """ % (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): with self.assertNumQueries(1): field = forms.ModelChoiceField(Category.objects.order_by('-name'))