#29865: Add XOR for use in Q Queries
-------------------------------------+-------------------------------------
Reporter: Griffith Rees | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: xor | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Ryan Heard):
Replying to [ticket:29865 Griffith Rees]:
> XOR seems to be available in [https://www.postgresql.org/docs/8.4/static
/functions-math.html Postgresql], [https://dev.mysql.com/doc/refman/8.0/en
/logical-operators.html MySQL], [https://docs.microsoft.com/en-us/sql/mdx
/xor-mdx?view=sql-server-2017 SequelServer] and
[https://docs.oracle.com/cd/E17952_01/mysql-5.1-en/logical-operators.html
Oracle] but NOT [https://stackoverflow.com/questions/2294137/sqlite-
exclusive-or-how-do-i-clear-a-flag sqlite]. Two stackoverflow questions
cover this sort of thing: [https://stackoverflow.com/questions/50408142
/django-models-xor-at-the-model-level] and
[https://stackoverflow.com/questions/14711203/perform-a-logical-exclusive-
or-on-a-django-q-object].
>
> I propose adding XOR to work with Q queries like the
[https://stackoverflow.com/a/21220712/678486 answer] to the second
question above. This will be my first time making a major contribution so
we'll see how this goes (apologies in advance if this is annoying!).
I started on this hoping to use it on my own postgres site, only to
realize that postgres does not support logical XOR. Too bad, as it would
help with not executing large subqueries multiple times.
Never-the-less I have created a PR with the proposed changes for this
[https://github.com/django/django/pull/14480 here], which probably needs
some TLC from a more advanced contributor. This code should add support
for XOR across the codebase, to both `Q` objects and `QuerySets`, and
ensure it gets down the SQL fed to the database.
Note that a TypeError is raised if XOR is attempted on an unsupported
backend. This seemed safer than converting on the fly to (A AND ~B) OR (~A
AND B), since doing that could lead to some unintended results when the
user is expecting XOR to be used. If it is decided that a conversion would
be more desirable, then the code can be changed.
--
Ticket URL: <https://code.djangoproject.com/ticket/29865#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/063.66ea15739e0a1e3d30b350267e01a7da%40djangoproject.com.