1
I havenβt used this field myself, but in base of what i read from documentaition, it should be like this:
from psycopg2.extras import DateTimeTZRange
Mymodel.objects.filter(
start_end_date__contained_by=DateTimeTZRange(
timezone.now(),
timezone.now() + timezone.timedelta(days=90)
)
)
to check if any start_end_date
field is in 90 days from now, you should also pass a datetime range.
edited:
from psycopg2.extras import DateTimeTZRange
Mymodel.objects.filter(
start_end_date__contained_by=DateTimeTZRange(
timezone.now(),
timezone.now() + timezone.timedelta(days=90),
start_end_date__lower_inc=False
)
)
1
As itβs a DateTimeRangeField
, I think your result can be achieved by using startswith
and endswith
just like that:
max_date = timezone.now() + timezone.timedelta(days=90)
MyModel.objects.filter(start_end_date__startswith__gte=timezone.now(), start_end_date__endswith__lte=max_date)
Hope it helps!
- [Answered ]-Sort feed by engagement count django
- [Answered ]-Django using i18n in URL patterns
- [Answered ]-Why can't I not use the now variable in bug fix for django poll tutorial part 5
- [Answered ]-How do I open this JSON data with Python?
-1
I think you could design the model more easily.
class MyModel(models.Model):
name = models.CharField(max_length=255)
start_date = models.DateTimeField()
end_date = models.DateTimeField()
Then you can find objects like the following.
target_time = timezone.now() + timezone.timedelta(days=90)
MyModel.objects.filter(start_date__lte = target_time).filter(end_date__gte = target_time)
- [Answered ]-Return a model from a custom query without hitting the database
- [Answered ]-How to obtain months in right order from different years from DateField in django?
- [Answered ]-Slugfield URL implementation in Django
- [Answered ]-Adding Custom Permissions in django.auth.models
- [Answered ]-Updating account balance with django
Source:stackexchange.com