Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Fwd: Aggregate class: a first-attempt
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Expand all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Honza Král  
View profile  
 More options Feb 22 2007, 9:34 pm
From: "Honza Král" <honza.k...@gmail.com>
Date: Fri, 23 Feb 2007 03:34:17 +0100
Local: Thurs, Feb 22 2007 9:34 pm
Subject: Fwd: Aggregate class: a first-attempt
I think we should really move this to the developers' list

On 2/23/07, Tim Chase <django.us...@tim.thechases.com> wrote:

yes, and that is how it should be in my opinion. Why have the database
calculate stuff you don't want?

> >>>> [
> >   {
> >     'name' : 'some name',
> >     'city' : 'Prague',
> >     'sum' : { 'pay' : 30000, 'some_other_field' : 10000 },
> >     'avg' : { 'pay' : 10000, 'age' : 30 },
> >     'count' : 5,
> >   },
> >   ......
> > ]

> As long as the resulting data can be easily accessed directly by
> a template, any number of resulting formats would work just fine.

good point, the dictionary one wouldn't work very well in templates

>  I'd see using it something like

> return render_to_response(
>         'test.html', {'items': Model.objects.all()}
>         )

> and then within the template refer to

>         <table>
>         {% for item in items %}
>                 <tr><td>{{ item.pay }}</td></tr>
>         {% endfor %}
>                 <tr><td>{{ items.aggregate.sum.pay }}</td></tr>
>         </table>

this just seems wrong - doing something like this in a template just
doesn't feel right, you should calculate the aggregation in the view -
its an expensive operation you should always think through

> or possibly

> items = Model.objects.all()
> return render_to_response('test.html', {
>         'items': items,
>         'stats': items.aggregate(),
>         })

I think this should be:

items = Model.objects.all()
return render_to_response('test.html', {
        'stats_by_owner': items.aggregate( ('owner',), count=True,
sum=('visits',) max=('rating', ), min=('rating', 'visits') ),
        })

{% for stat in stats_by_owner %}
  Owner {{ stat.owner }} has written {{ stat.count }} articles wit
ratings between {{ stat.min.rating }} and {{ stat.max.rating }}. His
work was read {{ stat.sum.visits }} times in total and his least
popular article was read {{ stat.min.visits }} times.
{% endfor %}

the problem here is that if this should make any sense, owner should
be an object, not just a field value (user id in this case), I believe
though that this can be done...

> and then within the template refer to

>         <table>
>         {% for item in items %}
>                 <tr><td>{{ item.pay }}</td></tr>
>         {% endfor %}
>                 <tr><td>{{ stats.sum.pay }}</td></tr>
>         </table>

> > it seems reasonably clean interface to me:
> > fist argument is a tuple of fields on which to call GROUP BY, then
> > there are several optional kwargs: avg, sum, min, max and count

the first argument should also be optional - in case you want the total

> > I am willing to code up a patch for this, it should be pretty
> > straightforward. If we would want to take it to the next level, we
> > could allow filter() on such aggregated queryset, that would manifest
> > itself in the HAVING clause...

> > what do you think??

> I like the ideas you present, particularly your clean interface
> for the aggregated results ("results.sum.fieldname" rather than
> my munged "results.fieldname_sum") and the clean interface for
> specific aggregation.  However, as stated above, I am hesitant to
> give up the simplicity of just asking for "aggregate everything
> possible" to prevent tight coupling.  Both sound good to me ("but
> I want both specific *and* generic" :)

and what about performance and optimization? do you really want to
calculate even the things you have no intention on using? Tight
coupling with the model here makes sense - you cannot really ask for
aggregation, if you don't know, what you want.

> Feel free to swipe any of the code I provided for any patches you
> make.

thanks, I will wait for some more feedback though, I want to get the
interface right before I start coding

> I think it will be a valuable addition as evaluation can be made
> lazy (thus, it has no great impact on existing code), and I've
> seen it requested several times in the Django-Users ML archives
> where the answer was "Django doesn't do that, but you can drop to
> hand-rolled SQL to do it for you."  I think it would help the
> folks that don't think in SQL.

or the guys that don't want to rely on one DB engine

> Just my musings...I appreciate your feedback.

same here ;)

> -tkc

--
Honza Král
E-Mail: Honza.K...@gmail.com
ICQ#:   107471613
Phone:  +420 606 678585

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »