Opened 6 years ago
Last modified 6 years ago
#30915 closed New feature
Implication for Q object — at Version 1
| 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 )
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.