summaryrefslogtreecommitdiff
path: root/tests/model_forms
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2021-09-08 13:57:49 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-09-09 12:07:59 +0200
commit0a28b42b1510b8093a90718bafd7627ed67fa13b (patch)
treedd8eb67d6e06b95d251a6aaeba6e54bf90f2f8e0 /tests/model_forms
parent46c8df640cfed5dd525ac1bcf5ad7e57b7ff2571 (diff)
downloaddjango-0a28b42b1510b8093a90718bafd7627ed67fa13b.tar.gz
Fixed #33084 -- Removed incorrect system check for ManyToManyField with limit_choices_to.
Diffstat (limited to 'tests/model_forms')
-rw-r--r--tests/model_forms/models.py17
-rw-r--r--tests/model_forms/tests.py17
2 files changed, 32 insertions, 2 deletions
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):