Extending count()

1 view
Skip to first unread message

Rock

unread,
Mar 1, 2006, 4:37:06 PM3/1/06
to Django developers
I have been sprinting this week with Jacob and others at PyCon.

Here is a small change that I am about to check in that extends the
count() function:

count(self,fieldname="*")

This change in query.py as well as the corresponding funcion in
manager.py extend the count() function. In the default case the
function is unchanged. If a fieldname is supplied, then the SQL query
is changed from COUNT(*) to COUNT(columnname).

The effect is that the count now returns the number of rows where the
named column has a non-null value.


Rock

Rock

unread,
Mar 2, 2006, 11:14:43 AM3/2/06
to Django developers
Russell Keith-Magee posted this in a separate thread:
> Article.objects.all().count() reads quite obviously as the count of
> all articles. However, Article.objects.all().count('title') reads (to
> me) like an inelegant way of saying 'a count of all titles'. This
> isn't what your proposal would return. What you are proposing can
> already be acheived using
>
> Article.objects.filter(fieldname__isnull=False).count()
>
> which, IMHO, reads better, and is more flexible.

I'll buy that. (Jacob and I were also considering the count(distinct
columnname)
SQL syntax and decided not to target that since it was already easy to
combine
distinct with count(). You point out that this logic also holds for
isnull. Point taken.)

I'll rework my patch for 1435 to remove that change.

Rock

Reply all
Reply to author
Forward
0 new messages