[Django] #30118: Add support for filter arguments in queryset.exists() and queryset.count()

3 views
Skip to first unread message

Django

unread,
Jan 18, 2019, 3:01:15 PM1/18/19
to django-...@googlegroups.com
#30118: Add support for filter arguments in queryset.exists() and queryset.count()
-------------------------------------+-------------------------------------
Reporter: Sjoerd | Owner: nobody
Job Postmus |
Type: New | Status: new
feature |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Currently, both .exists() and .count() do not support any arguments.

I would very much enjoy being able to write

{{{
if SomeModel.objects.exists(field=value):
}}}

instead of

{{{
if SomeModel.objects.filter(field=value).exists():
}}}

and similarly for count.

Especially when the "search" part of the query is more complex, it would
allow one to write

{{{
number_of_superusers_in_group = User.objects.count(
is_superuser=True,
group=group,
)
}}}

which I find visually more appealing over

{{{
number_of_superusers_in_group = User.objects.filter(
is_superuser=True,
group=group,
).count()
}}}

because of the strange "method-call" on the last line.

As it seems fairly obvious to me, and also fairly simple to implement, I
suspect there might be something I'm missing as to why it's not a good
idea, and it might already have been documented, but I can not find it.

--
Ticket URL: <https://code.djangoproject.com/ticket/30118>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 18, 2019, 3:50:10 PM1/18/19
to django-...@googlegroups.com
#30118: Add support for filter arguments in queryset.exists() and queryset.count()
-------------------------------------+-------------------------------------
Reporter: Sjoerd Job Postmus | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => wontfix


Comment:

This might have been proposed before but I couldn't find a discussion.

I doubt there would be consensus for breaking with the Zen of Python
("There should be one-- and preferably only one --obvious way to do it.")
but you can propose it on the DevelopersMailingList.

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

Django

unread,
Jan 18, 2019, 6:35:56 PM1/18/19
to django-...@googlegroups.com
#30118: Add support for filter arguments in queryset.exists() and queryset.count()
-------------------------------------+-------------------------------------
Reporter: Sjoerd Job Postmus | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix
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 Tim Graham):

* has_patch: 0 => 1


Comment:

On the [https://github.com/django/django/pull/10866 PR], Simon and Nick
also supported the wontfix decision.

--
Ticket URL: <https://code.djangoproject.com/ticket/30118#comment:2>

Reply all
Reply to author
Forward
0 new messages