Opened 6 years ago

Closed 6 years ago

#30270 closed Bug (invalid)

.all() for related manager always returns empty list

Reported by: Victor Porton Owned by: nobody
Component: Database layer (models, ORM) Version: 2.1
Severity: Release blocker 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

.all() for related manager always (at least in my case) returns empty list. This seems an big error or at least a wrongly documented or undocumented behavior.

It is demonstrated by the attached small test project.

The most relevant code fragment is:
`

holder = Holder.objects.get(pk=1)
self.assertEqual(holder.items.all().count(), 2, 'Correct objects count')

`

Django 2.1.7

Attachments (1)

bug.tar.gz (3.0 KB ) - added by Victor Porton 6 years ago.
Small project to test for the bug

Download all attachments as: .zip

Change History (2)

by Victor Porton, 6 years ago

Attachment: bug.tar.gz added

Small project to test for the bug

comment:1 by Tim Graham, 6 years ago

Resolution: invalid
Status: newclosed

I don't see an issue. Item.objects.create(holder=holder) populates the foreign key relation, not the many-to-many relation which is what holder.items.count() is querying. You could make the test case pass like this (adding holder.items.set([i1, i2])):

class BugTestCase(TestCase):
    def setUp(self):
        holder = Holder.objects.create()
        i1 = Item.objects.create(holder=holder)
        i2 = Item.objects.create(holder=holder)
        holder.items.set([i1, i2])

    def test_bug(self):
        """Test bug"""
        holder = Holder.objects.get(pk=1)
        self.assertEqual(holder.items.all().count(), 2, 'Correct objects count')
Note: See TracTickets for help on using tickets.
Back to Top