Tell me please. The model has such fields

class Tariffs(models.Model): days = IntegerRangeField(verbose_name=u'Дни') rate = models.DecimalField(verbose_name=u'Коэффициент', max_digits=5, decimal_places=2) 


The database contains records of days of different ranges (for example (0, 7)) and coefficients (rate). I get two dates from the user, I calculate the number of days between them and I want to check what range of fields this number falls in, then if found, multiply by the corresponding coefficient and then output. How to act? I use Django 1.10.
About QuerySet, filters, etc. read, I just can not understand what the query should look like.
Thanks for any suggestions.

  • The question did not understand. What kind of query you want to make? - Mr. Fix
  • @Mr.Fix In the database fields with data types int4range (days) and decimal (rate). I get the number of days from the user and I need to go to the database to see what range this day falls on and multiply by the corresponding coefficient. For example: the user entered two dates, I calculated the difference received 9 days. The database has intervals (0-7) with a coefficient. 2, (8-14) with a coefficient 2.2, (15-24) with a coefficient 3.2 etc. It turns out 9 falls into (8-14) - we multiply by 2.2, we get 19.8. Hope explained) - Fill

1 answer 1

PostgreSQL has range functions .
I needed to check which range the number is in (day). For this, the operator 'contains the element' @> .
In SQL, it looks like this SELECT day FROM tariffs WHERE days @> day . (where day = request.GET['day'] , I get from the user, in this case through GET; days has a range type of int4range )
Through Django ORM it turns out
query = Tariffs.objects.filter(days__contains=day).values('rate') . So I found the desired ratio.