For these derived values, I usually have an attribute of the model that 
is updated at save time. You could have an 'order_total' on your Order 
model, and whenever you save an OrderLine, it updates the order_total on 
its associated Order and saves the order. Then it just becomes another 
attribute of the order (albeit denormalised - although that's not such a 
bad thing in these cases as you'll save having to recalculate it every 
time.) Check out slide 17 onwards on the Django Masterclass presentation 
at http://toys.jacobian.org/presentations/2007/oscon/tutorial/

Hope that helps,

Mike


[EMAIL PROTECTED] wrote:
> Hi
>
> I have a fairly simple use-case which I feel should be achievable, but
> which I'm not able to pull off. What I want is the ability to modify
> the base query used by the admin to add some derived properties. For
> example, if you have Orders with corresponding OrderLines, it's useful
> to add the subtotal for each order in the order list. To achieve this
> I tried the following:
>
> class OrderAdminManager(models.Manager):
>     def get_query_set(self):
>         return super(OrderAdminManager, self).get_query_set().extra(
>             select={'order_total': '(select sum(quantity * item_price)
> from webshop_orderline where order_id = webshop_order.id)'})
>
> And in the inner Admin class I added this line:
>         manager = OrderAdminManager()
>
> The net result of this is that the query result contains the
> order_total column, and I can display it in the admin, but only if I
> do so using a custom method, which means no sorting and no filtering.
> If I try to add "order_total"  as a non-editable field it blows up
> when Django queries for webshop.order_total.
>
> Any ideas?
>
>
> >
>   


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to