Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.utils import timezone
- now = timezone.now()
- active_bookings = Booking.objects.filter(
- start_time__gte=now,
- end_time__lt=now
- )
- # myapp/models.py
- class Booking(models.Model):
- @property
- def is_active(self):
- now = timezone.now()
- return self.start_time >= now and self.end_time < now
- # In your view or in a utility function
- myBooking = Booking.objects.get(id=12345)
- print(myBooking.is_active)
- # myapp/management/commands/update_active_bookings.py
- from django.core.management.base import BaseCommand
- class Command(BaseCommand):
- def handle(self, *args, **kwargs):
- now = timezone.now()
- for booking in Booking.objects.all().iterator():
- is_active = self.start_time <= now and self.end_time > now
- if self.is_active != is_active:
- # Only saving the model if we have to
- self.is_active = is_active
- self.save()
- */10 * * * * source /path/to/venv/bin/activate && /path/to/app/manage.py update_active_bookings
Add Comment
Please, Sign In to add comment