Opened 6 years ago

Closed 6 years ago

#30103 closed New feature (duplicate)

Allow Q objects to be hashable, so that they can be used in sets

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

Description

Given:

>>> x = set()
>>> x.add(Q(a=1))
>>> x.add(Q(a=1))

I was *expecting* that the x would end up with {<Q: (AND: ('a', 1))>} but instead it ends up with {<Q: (AND: ('a', 1))>, <Q: (AND: ('a', 1))>}

For Q objects to work in sets (and as dictionary keys I guess) the class would need to implement __hash__ and __eq__ methods I think. This is slightly complicated by the fact it consumes all *args, **kwargs so doesn't necessarily have a concrete set of values to easily equality-check (and they could feasibly be nested, and in a different order [eg: Q(a=1, b=2) and Q(b=2, a=1))

Change History (2)

comment:1 by Keryn Knight, 6 years ago

Cc: Keryn Knight added

comment:2 by Simon Charette, 6 years ago

Resolution: duplicate
Status: newclosed

Confirmed this is a duplicate of #28629 which is fixed in Django 2.1.

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