[Django] #36981: Add PostgreSQL num_nonnulls (code provided)

13 views
Skip to first unread message

Django

unread,
Mar 12, 2026, 6:13:22 AMMar 12
to django-...@googlegroups.com
#36981: Add PostgreSQL num_nonnulls (code provided)
-------------------------------------+-------------------------------------
Reporter: Robin Christ | Type: New
| feature
Status: new | Component: Database
| layer (models, ORM)
Version: 6.0 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
I recently implemented "anyOf" / "tagged union" fields. I added
specialized CheckConstraints using num_nonnulls

The code looks like this:

{{{
class NumNonNulls(Func):
"""
Wraps PostgreSQL `num_nonnulls`.

Returns the number of non-null arguments as an integer. Can be used
with
`F()` references to model fields (including e.g. OneToOne)
relationships.

Example::

NumNonNulls(F("one_to_one_rel_1"), F("one_to_one_rel_1"))
-> SQL: num_nonnulls("one_to_one_rel_1_id",
"one_to_one_rel_2_id")
"""

function = "num_nonnulls"

def __init__(self, *expressions: object, **extra) -> None:
super().__init__(*expressions, output_field=IntegerField(),
**extra)


class NumNonNullsEq(Func):
"""
Renders ``num_nonnulls(<args>) = <n>`` as a boolean expression.

Suitable for use as the `condition` of a `CheckConstraint`.

Parameters
----------
*expressions:
The expressions (e.g. `F()` expressions) to pass to
`num_nonnulls`.
count:
The value or expression the result must equal.

Example::

NumNonNullsEq(F("one_to_one_rel_1"), F("one_to_one_rel_2"),
count=1)
-> SQL: num_nonnulls("one_to_one_rel_1_id", "one_to_one_rel_2_id")
= 1
"""

arg_joiner = " = "
template = "%(expressions)s"

def __init__(self, *expressions: object, count: object, **extra) ->
None:
if not expressions:
raise ValueError("NumNonNullsEq() requires at least one
expression.")

super().__init__(
NumNonNulls(*expressions),
count,
output_field=BooleanField(),
)
}}}

Is there any interest from Django's side in this?
--
Ticket URL: <https://code.djangoproject.com/ticket/36981>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 12, 2026, 11:03:38 AMMar 12
to django-...@googlegroups.com
#36981: Add PostgreSQL num_nonnulls (code provided)
-------------------------------------+-------------------------------------
Reporter: Robin Christ | Owner: Shubh Rai
Type: New feature | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Shubh Rai):

* cc: Shubh Rai (added)
* owner: (none) => Shubh Rai
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36981#comment:1>

Django

unread,
Mar 12, 2026, 12:26:12 PMMar 12
to django-...@googlegroups.com
#36981: Add PostgreSQL num_nonnulls (code provided)
-------------------------------------+-------------------------------------
Reporter: Robin Christ | Owner: Shubh Rai
Type: New feature | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Shubh Rai):

I have submitted a pr - https://github.com/django/django/pull/20899. I did
not add the test file but i tested it on my local, I did not add the test
file,as I believe the changes should be as minimal as possible. Please
review it and let me know if any changes are required.
--
Ticket URL: <https://code.djangoproject.com/ticket/36981#comment:2>

Django

unread,
Mar 12, 2026, 12:30:30 PMMar 12
to django-...@googlegroups.com
#36981: Add PostgreSQL num_nonnulls (code provided)
-------------------------------------+-------------------------------------
Reporter: Robin Christ | Owner: Shubh Rai
Type: New feature | Status: closed
Component: Database layer | Version: 6.0
(models, ORM) | Resolution:
Severity: Normal | needsnewfeatureprocess
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* easy: 1 => 0
* resolution: => needsnewfeatureprocess
* status: assigned => closed

Comment:

Thank you for your suggestion! When suggesting a new feature for Django,
the feature idea should first be proposed and discussed with the
community. To do that, please raise this on the [https://github.com/django
/new-features/issues ​new feature tracker].

I'll close the ticket for now, but if the community agrees with the
proposal, please return to this ticket and reference the forum discussion
so we can re-open it. For more information, please refer to
[https://docs.djangoproject.com/en/stable/internals/contributing/bugs-and-
features/#requesting-features the documented guidelines for requesting
features].

Thanks again!
--
Ticket URL: <https://code.djangoproject.com/ticket/36981#comment:3>

Django

unread,
Mar 12, 2026, 1:17:18 PMMar 12
to django-...@googlegroups.com
#36981: Add PostgreSQL num_nonnulls (code provided)
-------------------------------------+-------------------------------------
Reporter: Robin Christ | Owner: Shubh Rai
Type: New feature | Status: closed
Component: Database layer | Version: 6.0
(models, ORM) | Resolution:
Severity: Normal | needsnewfeatureprocess
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Shubh Rai):

thank you for the clarification I believe it falls in new feature
process.For now I am closing my pr, If in future this ticket gets accepted
I will reopen my pr.
--
Ticket URL: <https://code.djangoproject.com/ticket/36981#comment:4>
Reply all
Reply to author
Forward
0 new messages