Opened 2 years ago

Closed 17 months ago

#21696 closed Bug (wontfix)

Allow usage of objects with add_to_query() in filter() calls

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


There was a private method add_to_query() for objects in add_q(). Using this it was possible for the objects to add themselves to the query's where/having condition. This capability was removed from 1.6 as part of ORM refactorings.

I have seen at least half a dozen complaints/questions about this removal. So, clearly this feature was used more than I anticipated. It will be relatively easy to add similar capability to 1.6, though the API needs to change a little. This will be around 5 lines of code change with practically no risk of regressions. So, I am going to add the capability back unless somebody objects.

The API will likely be add_to_query(query, used_aliases, where_node) where it was previously add_to_query(query, used_aliases). The where_node addition is needed due to other changes in the ORM.

Change History (9)

comment:1 Changed 2 years ago by akaariai

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Ready for checkin

A pull request implementing add_to_query() support here: This needs release notes, but is otherwise ready for checkin. I'll mark it so that this will not be forgotten.

comment:2 Changed 22 months ago by akaariai

Actually, we likely don't want to add release notes for this one. add_to_query() was internal API and it will remain so. Hopefully we will get official API for .filter(MyCustomFilterCondition(...)) in Django 1.8. If that happens we will likely remove the add_to_query() hack again.

comment:3 Changed 22 months ago by akaariai

I updated PR 2125 with some more code. Now using add_to_query() objects is also possible and at least somewhat working in HAVING clauses, too.

Using the new version requires some changes to code, but it allows users who relied on add_to_query() to use 1.6 and 1.7. Currently upgrading to 1.6 or 1.7 seems really hard as there doesn't seem to be any viable upgrade path.

comment:4 Changed 22 months ago by akaariai

For some strange reason I get these errors after test suite has ran (likely on Python exit time):
OK (skipped=347, expected failures=8)
Destroying test database for alias 'default'...
Destroying test database for alias 'other'...
Exception KeyError: (<weakref at 0xeeb7578; dead>,) in <function remove at 0x28f9848> ignored
Exception KeyError: (<weakref at 0xeeb7418; dead>,) in <function remove at 0x27ecb90> ignored

This happens with the patch only on py27. py33 seems to be unaffected, and I don't get this error on master. I can't see anything in the patch that should cause this. Another pair of eyes could help spot what is happening here.

comment:5 Changed 22 months ago by loic84

@akaariai I seem to get these weakref errors intermittently as well on the current master, so it may not be your patch.

I suspect it has to do with the recent changes to bring Python 3.4 compat on signals.

comment:6 Changed 22 months ago by mjtamlyn

  • Triage Stage changed from Ready for checkin to Accepted

I'm also seeing those weakref teardown errors on this patch, but I've not seen them otherwise. This might be resolved if the patch is brought up to speed with master though.

As the patch does not apply cleanly, I'm going to remove the RFC flag.

comment:7 Changed 20 months ago by manfre

I've gotten weakref errors at the end of the test suite at various points in time without having applied this patch. I don't think the weakref errors should hold back this case.

comment:8 Changed 18 months ago by timo

  • Has patch set
  • Patch needs improvement set

comment:9 Changed 17 months ago by akaariai

  • Resolution set to wontfix
  • Status changed from new to closed

Time to merge this has passed. We will hopefully get official support for something similar in 1.8.

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