I have following model
JavaScript
x
11
11
1
class TimePeriod(BaseModel):
2
product = models.ForeignKey(to=Product, on_delete=models.CASCADE)
3
min_travel_days = models.PositiveIntegerField()
4
max_travel_days = models.PositiveIntegerField()
5
value = models.DecimalField(max_digits=19, decimal_places=10)
6
7
insurance_period_min_days = models.PositiveIntegerField(
8
null=True, blank=True)
9
insurance_period_max_days = models.PositiveIntegerField(
10
null=True, blank=True)
11
and Product model is as
JavaScript
1
3
1
class Product(BaseModel):
2
name = models.CharField(max_length=255)
3
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
JavaScript
1
3
1
TimePeriod.objects.filter(product__name='silver',insurance_period_min_days__lte=90,insurance_period_max_days__gte=90,
2
min_travel_days__lte=29,max_travel_days__gte=29).values('value').first()
3
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
JavaScript
1
2
1
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()
2