I have following model
class TimePeriod(BaseModel): product = models.ForeignKey(to=Product, on_delete=models.CASCADE) min_travel_days = models.PositiveIntegerField() max_travel_days = models.PositiveIntegerField() value = models.DecimalField(max_digits=19, decimal_places=10) insurance_period_min_days = models.PositiveIntegerField( null=True, blank=True) insurance_period_max_days = models.PositiveIntegerField( null=True, blank=True)
and Product model is as
class Product(BaseModel): name = models.CharField(max_length=255)
I have the following sheet to calculate the min_days and max_days and insurance min max days.
Insurance_period | Travel days | Value |
---|---|---|
1-92 days | 1-30 days | 30 |
1-183 days | 1-90 days | 45 |
1-365 days | 1-90 days | 50 |
1-365 days | 1-180 days | 60 |
My problem is suppose I have 29 days as travel days and 90 days as Insurance days How do I calculate the value by using django query? As the actual value for 90 days of insurance days and 29 days travel time is 30
.
I tried doing this way
TimePeriod.objects.filter(product__name='silver',insurance_period_min_days__lte=90,insurance_period_max_days__gte=90, min_travel_days__lte=29,max_travel_days__gte=29).values('value').first()
I am getting 4 different result as my query satisfies all condition of above table any way I can do to satisfy above table conditon? to get 30 as value?
Advertisement
Answer
I think I got the answer I was missing order by
TimePeriod.objects.filter(is_business=True,policy__slug='silver',insurance_period_min_days__lte=91,insurance_period_max_days__gte=91,min_days__lte=30,max_days__gte=30).values('value').order_by('value').first()