The documentation says, "All of the range fields translate to psycopg2 
Range objects <http://initd.org/psycopg/docs/extras.html#adapt-range> in 
python, but also accept tuples as input if no bounds information is 
necessary."

It looks to me like you could initialize the objects using psycopg's Range 
objects to eliminate the need to handle the tuple case. The tests have some 
examples: 
https://github.com/django/django/blob/master/tests/postgres_tests/test_ranges.py

On Tuesday, February 14, 2017 at 8:05:57 AM UTC-5, 
francois-xa...@airinov.fr wrote:
>
> 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
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/517e880b-17cc-4ef7-a68b-14fcd5ff5a4f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to