Opened 11 years ago

Closed 11 years ago

Last modified 10 years ago

#1579 closed defect (fixed)

Exclude in limit_choices_to doesn't work

Reported by: Rudolph Froger Owned by: Adrian Holovaty
Component: Core (Other) Version: magic-removal
Severity: major Keywords: limit_choices_to exclude
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I'm migrating my project to Magic Removal. I couldn't find how to do the equivalent of the old limit_choices_to = {'name__ne' : 'John'} in Magic Removal. This should be something like: limit_choices_to = {'name__exclude': 'John'} .
See the discussion at Google group django-users.

Change History (4)

comment:1 Changed 11 years ago by Luke Plant

Proposal: Change the implementation of limit_choices_to (and all places that accept a dictionary and pass it to .filter()) to accept a 'Q' object as an alternative. 'Q; objects can be detected by having the 'get_sql_clause' method (I believe it is currently '_get_sql_clause', but this doesn't make much sense -- it should be changed to be a public method instead of a semi-private one, since it is only ever used by external classes I think).

If it is a 'Q' object, then it is passed as a single positional parameter to filter(), instead of as keyword arguments. For the above use case, you can then do limit_choices_to = QNot(name='John') (IIRC). Since you can combine 'Q' objects, you can build up any arbitrary query this way, including queries that require custom 'Q'-type classes.

What do the other devs think? I'm happy to implement it.

comment:2 Changed 11 years ago by Luke Plant

I misremembered about _get_sql_clause (that's actually a method on QuerySets). Anyway, I'm working on this now, Adrian has given the go-ahead.

comment:3 Changed 11 years ago by Luke Plant

Resolution: fixed
Status: newclosed

(In [2850]) Fixed #1579 - added support for 'Q' objects in limit_choices_to.

comment:4 Changed 10 years ago by Adrian Holovaty

milestone: Version 0.92

Milestone Version 0.92 deleted

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