0

I have two models Event and Shift.

class Event(models.Model):
    start = models.DateTimeField()
    end = models.DateTimeField(blank=True, null=True)

class Shift(models.Model):
    name = models.CharField(max_length=20)
    start = models.TimeField()
    end = models.TimeField()

Now on some view I have to filter out Events that occur in the interval of a Shift. And I did this.

queryset = Event.objects.all()
queryset = queryset.filter(start__time__gte=s.start).filter(start__time__lte=s.end) |\
queryset.filter(start__time__lte=s.start).filter(end__time__gte=s.start)

And now I want to trim start and end of events in the queryset to be in the rage of the start and end of the Shift I'm filtering on. Also, some events may cross a Shift more than once. In that case, I have to split the Event to the number of times it crosses a Shift. How can I achieve this?

Dawit Abate
  • 462
  • 5
  • 10

0 Answers0