Opened 10 years ago

Closed 10 years ago

#22876 closed Bug (duplicate)

Related lookups allow models of a different type

Reported by: Ben Davis Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Django currently allows this:

>>> pig = Pig.objects.get(pk=1)
>>> duck = Duck.objects.get(pk=1)
>>> ducklings = Duckling.objects.filter(mother_duck=pig)
[<Duckling: Duckling object>, <Duckling: Duckling object>]

Unfortunately this can lead to baby ducklings being adopted by pigs.

A commit was made last year that was intended to fix this ( changeset:7cca8d5 ), but the test doesn't seem to be written correctly. It should assert that the resulting queryset is empty, not populated with ob:

  • tests/queries/tests.py

    diff --git a/tests/queries/tests.py b/tests/queries/tests.py
    index 60d1083..cb615bb 100644
    a b class RelatedLookupTypeTests(TestCase):  
    32473247        # be a good idea...
    32483248        self.assertQuerysetEqual(
    32493249            ObjectB.objects.filter(objecta=wrong_type),
    3250             [ob], lambda x: x)
     3250            [], lambda x: x)
    32513251        self.assertQuerysetEqual(
    32523252            ObjectB.objects.filter(objecta__in=[wrong_type]),
    3253             [ob], lambda x: x)
     3253            [], lambda x: x)

Change History (2)

comment:1 by Tim Graham, 10 years ago

Duplicate of #14334, I think.

comment:2 by Ben Davis, 10 years ago

Resolution: duplicate
Status: newclosed

Yep, the proposed patch there fixes the issue. Closing.

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