S
S
Stanislav Gordienko2016-10-07 07:04:05
Django
Stanislav Gordienko, 2016-10-07 07:04:05

How to implement from an architectural point of view the display of the cost with or without a discount in the model?

Tell me how to implement correctly from an architectural point of view the display of the cost of goods with or without a discount. There is a model in which two values ​​are stored. The user can activate the promo code and then we need to display the cost on the page depending on it. So, now I did the following. In the template, I check if a discount has been applied, and if so, then I display the discounted value, if not, then something else. But I don't like the solution. Somehow it looks awful. Maybe you need to make a method for the model, to which to pass the parameter whether there was a discount or not, and then already in this method to return the correct value? There may be other options that I'm not aware of.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-10-07
@stagor

  1. Make a model of promo codes that stores the promo code itself and the amount of discount it gives.
  2. In the product model, implement property current_price , which returns the value of the price field with a discount, if applicable, or just the value of the field, if not.
  3. In the template just output {{ item.current_price }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question