Opened 5 years ago

Closed 5 years ago

#30915 closed New feature (wontfix)

Implication for Q object.

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

Description (last modified by Bartłomiej Biernacki)

Starting with Django 2.2 we have CheckConstraints where we can use Q objects to create Database level checks.
I found that it's common to have some conditional check constrains i.e. "if some field_a has value 'x' then field_b have value greater than 0".
In basic logic we can write this as p => q which can be translated to ~p or q.
Using Django Q we can write this constraints as ~Q(field='x') or Q(field_b__gt=0).
While this serves the purpose it might be more readable if we will have a dedicated "implication" function.

I propose to use binary operator >> for that purpose, so in Django Q we could write a Q(field='x') >> Q(field_b__gt=0) which in mine opinion looks much more readable and can encourage people to use more database level constratins in their projects

I have provided a patch with this functionality: https://github.com/django/django/pull/11978
If this will be accepted I will add documentation to it.

Change History (3)

comment:1 by Bartłomiej Biernacki, 5 years ago

Description: modified (diff)

comment:2 by Bartłomiej Biernacki, 5 years ago

Has patch: set
Needs documentation: set
Needs tests: set

comment:3 by Mariusz Felisiak, 5 years ago

Resolution: wontfix
Status: assignedclosed
Summary: Implication for Q objectImplication for Q object.

Thanks for this ticket, however using a >> operator for implications sounds extremely confusing for me and as you pointed it can be described by ~p or q. I don't think that we need a separate API for this. You can start a discussion on DevelopersMailingList if you don't agree.

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