get() and filter() on related fields #4

tryolabs opened this Issue Feb 23, 2012 · 0 comments


None yet
2 participants

ghost commented Feb 23, 2012

There seems to be a problem with filter and get when referencing objects that are related, for example via a ForeignKey.

Imagine you have an Organization that is associated with multiple Groups (that is, in the Group model we have a ForeignKey that points to Organization). In the DB we have an organization named "My Organization", another one named "My Organization 2" and each of those have a (different) associated group called 'root', so there are actually two groups with the same name.

In this code:

org = Organization.objects.get(name='My Organization')
result_filter = org.groups.filter(name='root')
result_get = org.groups.get(name='root')

result_filter works ok and only returns a QuerySet with 1 group, but the get fails because it throws MultipleObjectsReturned exception. The get seems to find the two 'root' groups, which indicates that the appropriate filtering is not being applied.

We tracked down the problem and it seems the all_with_deleted() function in SoftDeleteManager is not applying the "core" filtering the RelatedManager is responsible for doing, so it is actually working over the entire DB and not the "org" QuerySet.

volrath pushed a commit to volrath/django-softdelete that referenced this issue Mar 28, 2012

Daniel Barreto
added 'core_filters' to `SoftDeleteManager.all_with_deleted` method w…
…hen we're dealing with a `RelatedManager`. Fixes issue #4

scoursen added a commit that referenced this issue Mar 28, 2012

@scoursen scoursen closed this Nov 29, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment