scoursen/django-softdelete — Issue #4: get() and filter() on related fields
Author: tryolabs
Created: 2012-02-23 17:03:50+00:00
State: open

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.