{{{#!python
class Number(models.Model):
value = models.IntegerField()
def __str__(self) -> str:
return f"{self.value}"
class NumbersToDiceThroughModel(models.Model):
number = models.ForeignKey("Number", on_delete=models.PROTECT)
die = models.ForeignKey("Die", on_delete=models.CASCADE)
class Die(models.Model):
numbers = models.ManyToManyField(
"Number",
through="NumbersToDiceThroughModel",
limit_choices_to=models.Q(value__gte=1),
)
}}}
Results in a **correct** limitation of possible choices. But at the same
time the following warning is issued:
{{{
... .Die.numbers: (fields.W343) limit_choices_to has no effect on
ManyToManyField with a through model.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33084>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: Carlton Gibson, Simon Charette (added)
* stage: Unreviewed => Accepted
Comment:
Thanks for the report!
I'm puzzled. I checked with a sample project and the following test:
{{{
class MyTests(TestCase):
def test_limit_choices_through(self):
for i in range(-10, 10):
Number.objects.create(value=i)
class CharacterDetailsForm(forms.ModelForm):
class Meta:
model = Die
fields = ['numbers']
self.assertEqual(Number.objects.count(), 20)
form = CharacterDetailsForm()
self.assertEqual(form.fields['numbers'].queryset.count(), 9)
self.assertEqual(form.fields['numbers'].queryset.filter(value__lt=1).count(),
0)
}}}
and `limit_choices_to` works since at least Django 1.1 (yes **1.1**, I
really tried to find when it wasn't working).
It seems that this check is incorrect since its introduction in
ba53da894fc713629ae4d3fbdd14eff98c808389 (see #26796) and the
[https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.ManyToManyField.limit_choices_to
related note] in docs is also incorrect since its introduction in
c6c25adf6d9f71ea11f61392f6f3d221f01e5216 (see #9733).
--
Ticket URL: <https://code.djangoproject.com/ticket/33084#comment:1>
* owner: nobody => Hasan Ramezani
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/33084#comment:2>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/14842 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/33084#comment:3>
Comment (by Simon Charette):
I'm puzzled as well Mariusz, I could swear ''something'' was not working
properly when I filed this ticket but it was too long ago for me to
remember the proper context.
--
Ticket URL: <https://code.djangoproject.com/ticket/33084#comment:4>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33084#comment:5>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/33084#comment:6>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"0a28b42b1510b8093a90718bafd7627ed67fa13b" 0a28b42]:
{{{
#!CommitTicketReference repository=""
revision="0a28b42b1510b8093a90718bafd7627ed67fa13b"
Fixed #33084 -- Removed incorrect system check for ManyToManyField with
limit_choices_to.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33084#comment:7>