There are three kinds of numeric
RangeFields in Python.
FloatField. They convert to
NumericRanges, but accept input as native Python tuples. The lower bound is included and the upper bound is excluded.
class Book(models.Model): name = CharField(max_length=200) ratings_range = IntegerRange()
It's simpler and easier to input values as a Python tuple instead of a
Book.objects.create(name='Pro Git', ratings_range=(5, 5))
Alternative method with
Book.objects.create(name='Pro Git', ratings_range=NumericRange(5, 5))
This query selects all books with any rating less than three.
bad_books = Books.objects.filter(ratings_range__contains=(1, 3))
This query gets all books with ratings greater than or equal to zero and less than six.
all_books = Book.objects.filter(ratings_range_contained_by=(0, 6))
This query gets all overlapping appointments from six to ten.
This query selects all books with any rating greater than or equal to four.
maybe_good_books = Books.objects.filter(ratings_range__contains=(4, None))
from datetime import timedelta from django.utils import timezone from psycopg2.extras import DateTimeTZRange # To create a "period" object we will use psycopg2's DateTimeTZRange # which takes the two datetime bounds as arguments period_start = timezone.now() period_end = period_start + timedelta(days=1, hours=3) period = DateTimeTZRange(start, end) # Say Event.timeslot is a DateTimeRangeField # Events which cover at least the whole selected period, Event.objects.filter(timeslot__contains=period) # Events which start and end within selected period, Event.objects.filter(timeslot__contained_by=period) # Events which, at least partially, take place during the selected period. Event.objects.filter(timeslot__overlap=period)