Calculate fields in view and pass the query to the template

0

I am using Django 1.4 and I have a query that brings several data from the db, including one that can have 3 different values (type). Depending on this data I make a conditional and calculate some values (accommodation and diet) to send to the template. If I do it with only one line in the data it works well, the problem is when I have several lines in which if I respect the data that comes in the recordset (name, date and type) but in the fields that calculates puts me in all lines the same.

This is what I want.

nombre        tipo mes     alojamiento        dieta
nombre_1       1   enero   50                 45
nombre_2       2   febrero 60                 80

Where:

  • accommodation = days * amount_hotel --- days depend on type.
  • diets = days * amount_diet ---- the two values depend on the type.

This is what I have.

nombre        tipo mes     importe_noche importe_dieta
nombre_1       1   enero   60                 80
nombre_2       2   febrero 60                 80

I've tried it with aggregate , but it gives me an error because the data to be calculated is not in the database.

    
asked by Victor Sanchez 24.02.2018 в 07:22
source

1 answer

0

For several lines you have to use the "annotate", it works in the same way as the "agregate" but it can be grouped in several rows, if you put your models and the query I can help you more specifically

    
answered by 02.03.2018 в 08:43