python - Getting total price for a ManyToManyField in Django -
i'm trying total price manytomanyfield. i'm getting each individual price on price_for_each_item , want total of many items in added 'total_price'
class purchaseorder(models.model): number_of_products = models.manytomanyfield('product', null =true) total_price = models.floatfield(verbose_name='total price') class product(models.model): products = models.charfield(max_length=256, null =true) price_for_each_item = models.floatfield(verbose_name = "price") def __unicode__(self): return self.products for example, if made order of macbook pro =$500, asus computer =$200, , mousepad= $50. i'd want total of go total_price = $750
edit: tried doing this, no avail. have idea i'd have fix work? when try display it says no float object callable
def get_total_price(self): return ([p.price_for_each_item p in self.total_price()])
moved above.
if understand correctly, retrieve sum object itself. if have order called "order" products assigned (during saving process, should able like:
order.total_price = order.number_of_products.all().aggregate(sum('price_for_each_item')). let me know if answers question , i'll move answer.
glad help. not nazi here, please give more thought model field names in future, confusing.
edit re comment:
i'm not sure think overriding model's save method way go work in admin:
def save(self): super(person, self).save() self.total_price = self.number_of_products.all().aggregate(sum('price_for_each_item')). self.save(commit=true) haven't tested @ though. have @ this: http://ifacethoughts.net/2009/07/14/calculated-fields-in-django/ if works, accept answer.
Comments
Post a Comment