Opened 3 years ago

Closed 3 years ago

#33084 closed Bug (fixed)

Removed incorrect fields.W343 check and notes about limit_choices_to restrictions for ManyToManyField.

Reported by: jhbuhrman Owned by: Hasan Ramezani
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords: limit_choices_to
Cc: Carlton Gibson, Simon Charette Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The model setup given below:

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.

Change History (7)

comment:1 by Mariusz Felisiak, 3 years ago

Cc: Carlton Gibson Simon Charette added
Summary: Incorrect warning W343 "limit_choices_to has no effect on ManyToManyField with a through model"Removed incorrect fields.W343 check and notes about limit_choices_to restrictions for ManyToManyField.
Triage Stage: UnreviewedAccepted

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 related note in docs is also incorrect since its introduction in c6c25adf6d9f71ea11f61392f6f3d221f01e5216 (see #9733).

comment:2 by Hasan Ramezani, 3 years ago

Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:3 by Hasan Ramezani, 3 years ago

Has patch: set

comment:4 by Simon Charette, 3 years ago

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.

comment:5 by Mariusz Felisiak, 3 years ago

Patch needs improvement: set

comment:6 by Hasan Ramezani, 3 years ago

Patch needs improvement: unset

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 0a28b42:

Fixed #33084 -- Removed incorrect system check for ManyToManyField with limit_choices_to.

Note: See TracTickets for help on using tickets.
Back to Top