imolitor
imolitor

Reputation: 798

How can I extract parts of a Django Postgres DateRangeField

I would appreciate some help. I can't get my head around this.
I want to use the Django Postgres DateRangeField for a model in my app.
It will hold two dates: a from-date and a to-date like in a booking system.

from django.contrib.postgres.fields import DateRangeField

class Booking(models.Model)
    from_to = DateRangeField()

All works fine and nicely. I can play around with it in the Admin.
In order to confirm the booking, I'd like to extract the 2 dates in variables again, and that's where I hit a wall :)

The printed object looks a lot like a tuple to me, with from-date and to-date and the bounds-definition

DateRange(datetime.date(2017, 2, 1), datetime.date(2017, 3, 31), '[)')

however, any attempt to extract one of the dates fails.

fromdate = self.from_to(0)
'DateRange' object is not callable

I'm sure it's simple, however, there is no documentation about that (at least not any I could find) Your help is more than appreciated.

Upvotes: 3

Views: 686

Answers (1)

Wilfried
Wilfried

Reputation: 1633

http://initd.org/psycopg/docs/extras.html#psycopg2.extras.Range

Object attributes:

lower
The lower bound of the range. None if empty or unbound.

upper
The upper bound of the range. None if empty or unbound.

Upvotes: 5

Related Questions