python - How to set non-model field property to all the objects in ManyToMany Relation? -
i have django model, manytomany relation, , through recursive function trying set custom property on main model , models in manytomany fields, manytomany related model property not getting reflected templates.
here code snippet:
def update_product_price_details(product): product.sale_price = product.original_price if product.product_type == "packaged_product": product_discount = productservices.get_product_discount(product) # todo : handle multiple items # calculate product sale price base on subproducts , disocunt product.original_price = 0 sub_product in product.packagedproduct.sub_products.all(): sub_product = productservices.update_product_price_details(sub_product) product.original_price = product.original_price + sub_product.sale_price if product_discount , product_discount.is_valid(): product.sale_price = product.original_price - (product.original_price*product_discount.discount_percentage/100) else: product_discount = productservices.get_product_discount(product) # todo : handle multiple items # if valid discount set new price sale_price if product_discount , product_discount.is_valid(): product.sale_price = product.original_price - (product.original_price*product_discount.discount_percentage/100) return product
code explanation
i pass product model update_product_price_details(product)
, each sub product calculating sale_price
, calculate pacakged_product
sale_price
.
sub_product = productservices.update_product_price_details(sub_product)
the value setting sub_product via above line of code not reflected actual product model.
is there way update manytomany field model custom property? totally stuck here, please help.
many many thanks.
making use of model functions simple solution it, nice , clean.
Comments
Post a Comment