On Tue, Aug 18, 2009 at 10:48 PM, mettwoch<mettw...@pt.lu> wrote:
>
> The whole thing behind that it is: I'm developing an invoicing/
> inventory application and I run into performance problems that might
> require using raw SQL and admittedly I'm not an SQL expert and I
> dislike the idea of doing so.
>
> I thought more on an approach of "SQL view" than denormalisation or a
> pseudoattribute. I've also read a little about F() and the extra()
> function to inject SQL, but that's to be done for every query while
> I'm more interested on having the calculation (business rule) defined
> once in the model.
>
> I could also access a view in PostGres where 'total' is defined as
> 'price*quantity' but as far as I know (and my SQL knowledge is rather
> poor), views are not updatable.
>
> The example was a quick and dirty draft and should read:
>
> from django.db import models
>
> class InvoiceLine(models.Model):
>    price = models.DecimalField()
>    quantity = models.DecimalField()
>    total = models.ExpressionField('price * quantity')
>
> The SQL looks something like this for InvoiceLine.objects.all():
>
> SELECT price, quantity, price * quantity AS total FROM invoiceline;

Ok - that's a little clearer.

This proposal has overlap with two others:

Firstly, #10972 - Using expressions with annotate(). This would allow
you to issue the following query:

InvoiceLine.objects.aggregate(Sum(F('price') * F('quantity'))

Secondly, an idea that doesn't have a ticket (which is something I
should probably correct) - annotating non-aggregate clauses. That is,
you should be able to do the following:

InvoiceLine.objects.annotate(total=F('price') *
F('quantity')).aggregate(Sum('total'))

This creates a 'total' column and attribute that isn't part of an
aggregate clause

With an implementation of this second idea, you don't need to have an
ExpressionField - you can just override the manager to have a default
query set that adds the annotation.

Both of these should be possible without too much effort - it's just a
matter of someone writing the code.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to