From 0a28b42b1510b8093a90718bafd7627ed67fa13b Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Wed, 8 Sep 2021 13:57:49 +0200 Subject: Fixed #33084 -- Removed incorrect system check for ManyToManyField with limit_choices_to. --- tests/model_forms/models.py | 17 +++++++++++++++++ tests/model_forms/tests.py | 17 +++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) (limited to 'tests/model_forms') diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py index 4e9ed2b1e4..c0b3a64148 100644 --- a/tests/model_forms/models.py +++ b/tests/model_forms/models.py @@ -480,3 +480,20 @@ class NullableUniqueCharFieldModel(models.Model): email = models.EmailField(blank=True, null=True) slug = models.SlugField(blank=True, null=True) url = models.URLField(blank=True, null=True) + + +class Number(models.Model): + value = models.IntegerField() + + +class NumbersToDice(models.Model): + number = models.ForeignKey('Number', on_delete=models.CASCADE) + die = models.ForeignKey('Dice', on_delete=models.CASCADE) + + +class Dice(models.Model): + numbers = models.ManyToManyField( + Number, + through=NumbersToDice, + limit_choices_to=models.Q(value__gte=1), + ) diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 9931fa50e3..7907aa1c3d 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -21,10 +21,10 @@ from django.test.utils import isolate_apps from .models import ( Article, ArticleStatus, Author, Author1, Award, BetterWriter, BigInt, Book, Category, Character, Colour, ColourfulItem, CustomErrorMessage, CustomFF, - CustomFieldForExclusionModel, DateTimePost, DerivedBook, DerivedPost, + CustomFieldForExclusionModel, DateTimePost, DerivedBook, DerivedPost, Dice, Document, ExplicitPK, FilePathModel, FlexibleDatePost, Homepage, ImprovedArticle, ImprovedArticleWithParentLink, Inventory, - NullableUniqueCharFieldModel, Person, Photo, Post, Price, Product, + NullableUniqueCharFieldModel, Number, Person, Photo, Post, Price, Product, Publication, PublicationDefaults, StrictAssignmentAll, StrictAssignmentFieldSpecific, Student, StumpJoke, TextFile, Triple, Writer, WriterProfile, test_images, @@ -2896,6 +2896,19 @@ class LimitChoicesToTests(TestCase): [self.marley, self.threepwood], ) + def test_limit_choices_to_m2m_through(self): + class DiceForm(forms.ModelForm): + class Meta: + model = Dice + fields = ['numbers'] + + Number.objects.create(value=0) + n1 = Number.objects.create(value=1) + n2 = Number.objects.create(value=2) + + form = DiceForm() + self.assertCountEqual(form.fields['numbers'].queryset, [n1, n2]) + class FormFieldCallbackTests(SimpleTestCase): -- cgit v1.3