-1

I have a model where i have a field called date range. which returns a range of date range both the date range is same but the time changes how can i access one date from range in the model using ORM.

class My_model(models.Model):
     date_range = ranges.DateTimeTZRange()
jimmy
  • 45
  • 9

1 Answers1

0

It looks like it can be accessed with upper and lower

obj = My_model.objects.all().first()
obj.date_range.upper
obj.date_range.lower

That field also inherents from RangeField It's all in contrib\postgres\fields\ranges.py if that helps at all

And here's some Query Stuff I you need: https://docs.djangoproject.com/en/3.2/ref/contrib/postgres/fields/#querying-range-fields


Edit, Aggregate

Source: Django/Postgres: Aggregate on RangeField

from django.db.models.functions import Upper, Lower
aggrDict = My_model.objects.all().aggregate(
    low=Min(Lower('date_range')),
    high=Max(Upper('date_range'))
)
print(aggrDict['low'])
print(aggrDict['high'])

Edit 2, filter

If you just want to return items that are 20 days away you could do:

from datetime import datetime, timedelta
twenty_days_in_the_future = (datetime.now()+timedelta(days=20)).date()

from django.db.models import Case, When, Value, IntegerField
My_model.objects.filter(date_range__startswith=twenty_days_in_the_future)


# It might need a __date, idk
My_model.objects.filter(date_range__startswith__date=twenty_days_in_the_future)

All Range filters: https://docs.djangoproject.com/en/3.2/ref/contrib/postgres/fields/#querying-range-fields

Nealium
  • 2,025
  • 1
  • 7
  • 9
  • it works exactly how you queried but with filter queryset it throws object has no attribute date_range – jimmy Sep 07 '22 at 17:24
  • does it say **Queryset** object has no attribute?- then we want an `aggregate` function – Nealium Sep 07 '22 at 17:26
  • Added Aggregate (I missed the `.all()` ) on first upload, that's why I'm making this comment – Nealium Sep 07 '22 at 17:32
  • First of all Thanks for your answers i actually need to check if todays date is 20 days before date_range and i have some other fields in my filtering query which i check for – jimmy Sep 07 '22 at 17:33
  • I added the filter, you could also tac on the `__gte` or `__lte` (greater than / equal and less than / equal) ..hopefully I'm not misunderstanding the **check** ..there is a way, with conditional statements, to have an annotated Boolean for if it's 20 days away.. I can provide that, idk how much use you'd get from it tho – Nealium Sep 07 '22 at 17:55
  • Unsupported lookup 'lower' for DateTimeRangeField or join on the field not permitted. I used both your queries. – jimmy Sep 08 '22 at 01:13
  • try `My_model.objects.filter(date_range__startswith=twenty_days_in_the_future)` ..I probably should have looked at my own link i put in there- in the original post there's a link for all the range queries – Nealium Sep 08 '22 at 01:50
  • The query is returning empty query set although i have changed days to 84 to match 'start_end_range': DateTimeTZRange(datetime.datetime(2022, 11, 30, 20, 0), datetime.datetime(2022, 11, 30, 20, 30) – jimmy Sep 08 '22 at 02:02