[Django] #33084: Incorrect warning W343 "limit_choices_to has no effect on ManyToManyField with a through model"

23 views
Skip to first unread message

Django

unread,
Sep 6, 2021, 8:03:24 AM9/6/21
to django-...@googlegroups.com
#33084: Incorrect warning W343 "limit_choices_to has no effect on ManyToManyField
with a through model"
-------------------------------------+-------------------------------------
Reporter: jhbuhrman | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords: limit_choices_to
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The model setup given below:

{{{#!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.

Django

unread,
Sep 7, 2021, 1:49:47 AM9/7/21
to django-...@googlegroups.com
#33084: Removed incorrect fields.W343 check and notes about limit_choices_to
restrictions for ManyToManyField.

-------------------------------------+-------------------------------------
Reporter: jhbuhrman | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: limit_choices_to | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Django

unread,
Sep 8, 2021, 2:57:57 AM9/8/21
to django-...@googlegroups.com
#33084: Removed incorrect fields.W343 check and notes about limit_choices_to
restrictions for ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: jhbuhrman | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: limit_choices_to | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* owner: nobody => Hasan Ramezani
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/33084#comment:2>

Django

unread,
Sep 8, 2021, 8:18:28 AM9/8/21
to django-...@googlegroups.com
#33084: Removed incorrect fields.W343 check and notes about limit_choices_to
restrictions for ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: jhbuhrman | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: limit_choices_to | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/14842 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/33084#comment:3>

Django

unread,
Sep 8, 2021, 9:01:59 AM9/8/21
to django-...@googlegroups.com
#33084: Removed incorrect fields.W343 check and notes about limit_choices_to
restrictions for ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: jhbuhrman | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: limit_choices_to | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 9, 2021, 12:30:38 AM9/9/21
to django-...@googlegroups.com
#33084: Removed incorrect fields.W343 check and notes about limit_choices_to
restrictions for ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: jhbuhrman | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: limit_choices_to | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/33084#comment:5>

Django

unread,
Sep 9, 2021, 5:08:48 AM9/9/21
to django-...@googlegroups.com
#33084: Removed incorrect fields.W343 check and notes about limit_choices_to
restrictions for ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: jhbuhrman | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: limit_choices_to | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/33084#comment:6>

Django

unread,
Sep 9, 2021, 7:17:37 AM9/9/21
to django-...@googlegroups.com
#33084: Removed incorrect fields.W343 check and notes about limit_choices_to
restrictions for ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: jhbuhrman | Owner: Hasan
| Ramezani
Type: Bug | Status: closed

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: limit_choices_to | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages