简体   繁体   中英

Django Queryset filter objects between 2 models iterating my values?

I'm doing a tax calculator and I have to do queries of one model and value them in another.

I explain what I want to do.

I have the following Models:

class Tarifa_Sem(models.Model):
    limite_inferior_isr = models.DecimalField(max_digits=10, decimal_places=2)
    limite_superior = models.DecimalField(max_digits=10, decimal_places=2)

class Calculadora_isr(models.Model):
    tarifa = models.ForeignKey(Tarifa_Sem, on_delete=models.CASCADE, null=True, blank=True, related_name='calculators')
    base_gravada = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
    limite_inf_calculo = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)

Now the problem in question is that I need to do the following.

My model Tarifa_Sem has the following data:

 +--------------------------------------+ 
 |          Model Tarifa_Sem            |
 +--------------------------------------+ 
 |id |limite_inf |  limite_superior     |
 +---+-----------+----------------------+
 | 1 |  0.01     |       133.21         |
 +---+-----------+----------------------+
 | 2 |  133.22   |       407.33         |
 +---+-----------+----------------------+
 | 3 |  133.22   |       610.96         |
 +---+-----------+----------------------+
 | 4 |  133.22   |       799.68         |
 +---+-----------+----------------------+
 | 5 |  133.22   |       814.66         |
 +---+-----------+----------------------+
 | 6 |  133.22   |      1023.75         |
 +---+-----------+----------------------+
 | 7 |  133.22   |      1086.19         |
 +---+-----------+----------------------+
 | 8 |  133.22   |      1130.64         |
 +---+-----------+----------------------+
 | 9 |  1130.65  |      1228.57         |
 +---+-----------+----------------------+
 | 10|  1130.65  |      1433.32         |
 +---+-----------+----------------------+
 | 11|  1130.65  |      1638.07         |
 +---+-----------+----------------------+
 | 12|  1130.65  |      1699.88         |
 +---+-----------+----------------------+

In my Calculator model I have the following data inside base_gravda:

 +--------------------------------------+ 
 |        Model Calculadora_isr         |
 +--------------------------------------+ 
 |id |base_gravada| limite_inf_calculo  |
 +---+------------+---------------------+
 |1  |  1000.00   |                     |
 +---+------------+---------------------+
 |2  |  1200.00   |                     |
 +---+------------+---------------------+
 |3  |  500.00    |                     |
 +---+------------+---------------------+
 |4  |  1600.00   |                     |
 +---+------------+---------------------+

I want to evaluate each of the base_gravada amounts within my model Tarifa_Sem, upper limit and that I return the first value that I bring by doing the following valuation base_gravada > = limite_superior and I make a first () so that I can return the first value that is greater that my base_gravada, once I have this value I tell him to bring me the limite_inferior and this is what I should keep in the model, the table with the results should look like this:

 +--------------------------------------+ 
 |         Model Calculadora_isr        |
 +--------------------------------------+ 
 |id |   Base    |  limite_inf_calculo  |
 +---+-----------+----------------------+
 |1  |  1000.00  |        133.22        |
 +---+-----------+----------------------+
 |2  |  1200.00  |       1130.65        |
 +---+-----------+----------------------+
 |3  |  500.00   |        133.22        |
 +---+-----------+----------------------+
 |4  |  1600.00  |       1130.65        |
 +---+-----------+----------------------+

That is, the first value of base_gravada 1000.00 places it in the range ID 6 of Tarifa_Sem and brings the limite_inferior_isr and so on with each value.

Now this is the closest I have been to solving the problem if someone can tell me what is missing.

for base in Calculadora_isr.objects.all():
    Tarifa_Sem.objects.filter(Q(limite_superior__gte=base.base_gravada)).values_list('limite_inferior_isr', flat=True).first()

This query is executed in the Django shell, it returns the following values:

Decimal('133.22')
Decimal('1130.65')
Decimal('133.22')
Decimal('1130.65')

These values ​​are correct, the problem is when I try to pass my results to a queryset, only the last value returns:

for base in Calculadora_isr.objects.all():
        queryset = Tarifa_Sem.objects.filter(Q(limite_superior__gte=base.base_gravada)).values_list('limite_inferior_isr', flat=True).first()

Decimal('1130.65')

Someone has an idea of ​​what is missing or how to solve this problem.

Thank you

Try this:

results = [
    Tarifa_Sem.objects.filter(
        limite_superior__gte=obj.base_gravada
    ).values_list('limite_inferior_isr', flat=True).first()
    for obj in Calculadora_isr.objects.all()
]

To update:

for obj in Calculadora_isr.objects.all():
    obj.limite_inf_calculo = Tarifa_Sem.objects.filter(
            limite_superior__gte=obj.base_gravada
        ).values_list('limite_inferior_isr', flat=True).first()
    obj.save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM