You probably have a list of products somewhere, right? (from a form or other
input). Couldn't you just do

sum([p.price for p in products])

where products is that list??


On Fri, Aug 29, 2008 at 9:08 PM, Koen Bok <[EMAIL PROTECTED]> wrote:

>
> I'd like to build relations with objects without saving to pre-
> determine the price for a proposed order based on a set of products.
> But when I add a Product project to the Order, I first have to save
> the Order. Is there any way around this?
>
> Example:
>
> class Order(Model):
>        id = models.AutoField(primary_key=True)
>        number = models.IntegerField()
>
>        def total(self):
>                return sum([p.price for p in self.product_set])
>
> class Product(Model):
>        id = models.AutoField(primary_key=True)
>        order = models.ForeignKey(Order)
>        price = models.DecimalField()
>
> order = Order(number=1)
>
> # Now the nex line throws an error if we did not save the order here
> order.product_set.create(Product(price=20.0))
>
> assert order.total() == 20.0
> >
>

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