Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#30118 closed New feature (wontfix)

Add support for filter arguments in queryset.exists() and queryset.count()

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

Description

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.

Change History (2)

comment:1 by Tim Graham, 6 years ago

Resolution: wontfix
Status: newclosed

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.

comment:2 by Tim Graham, 6 years ago

Has patch: set

On the PR, Simon and Nick also supported the wontfix decision.

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