On Apr 23, 7:23 pm, "Jorge Vargas" <[EMAIL PROTECTED]> wrote:
> 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.
No worries, you were right, I just didn't get it.
>
> > 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.
Well that's not very good news, considering the way I'm doing it
doesn't work :-)
> 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()
That's more or less what I'm having to do right now. My goal was to
keep the code that consumes this model from having to explicitly
call .save() every time the order has an item added to it. The item
gets saved and related to an order when order.items.create() is
called, so there's no reason (in my brain) why the total should not
immediately reflect that change, thus my desire to call
self.order.save() from Item.save(). Right now I just make sure to
call order.save() after every order.items.create(), but it feels like
a bad workaround for my desired behavior.
All that being said, I don't need any more of a resolution for this,
since I have the workaround, but I'm still *very* open to ideas about
how it can be done differently.
Thanks,
Don Spaulding
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---