Hello all,
I've been hitting a small annoyance when using the RangeField classes in the PostgreSQL contrib modules : while the value returned by psycopg is a psycopg.extras.Range type, the Django documentation explicitly mentions using tuples when creating and updating model instances.
This means that any method or property that has to process RangeFields must handle both tuples and Range types, which are incompatible.
For example, here is a very simple model :
class MyModel(models.Model):
range = DateTimeRangeField()
def has_started_1(self):
return self.range.lower() <= timezone.now()
def has_started_2(self):
return self.range[0] <= timezone.now()
In this naïve implementation, the `has_started_1` method will crash when used like this :
instance = MyModel.objects.create(range=(timezone.now(), timezone.now() + timezone.timedelta(days=5))
instance.has_started_1() # Crash
instance.has_started_2() # True
...and the `has_started_2` method will crash with this :
instance = MyModel.objects.last()
instance.has_started_1() # True
instance.has_started_2() # Crash
How should I handle that better?
Thanks for your help!
François-Xavier