On Wed, Apr 23, 2008 at 11:09 AM, Don Spaulding
<[EMAIL PROTECTED]> wrote:
>
>
>
>  On Apr 22, 11:24 pm, Malcolm Tredinnick <[EMAIL PROTECTED]>
>  wrote:
>
> > On Tue, 2008-04-22 at 13:47 -0700, Don Spaulding wrote:
>  >
>  > [...]
>  >
>  > > I try to do something like this:
>  >
>  > > order = Order()
>  > > order.save()
>  > > order.items.create(price=Decimal("5.00"))
>  > > order.total         #gives me 0
>  > > order.save()
>  > > order.total         #gives me the correct order.total
>  >
>  > > My guess is that even though I've saved the item before calling
>  > > order.save(), the order objects still doesn't know that the new item
>  > > is related to it.  Anybody know if this is expected behavior?
>  >
>  > Yes, it is expected. As Jorge noted, the "order" instance is a snapshot
>  > of the data at one moment in time, not a live update.
>  >
>  Thanks Malcolm and Jorge.  I didn't understand Jorge's response the
>  first couple times I read it, but now it seems a bit clearer.
>
sry for that sometimes I sound confusing.

>  I can accept that the item's self.order is stale in Item.save().  But
>  my use case is still "Every time I add an item to an order, the
>  order's total needs updated".  How can I force the order to hit the DB
>  again to find all of its (freshly) related objects?
I believe the only way is how you are doing it. now let me ask a
question why you need that? can't this be done in the controller? are
you updating the price total many times per call? why not do it all in
memory and then save ones? if you take all that out of the model and
into the controller (maybe even a function) you could do something
like this.
new_order = Order()
new_order.total = sum(new_order.items.all())
order.save()

or I'm missing something?
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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