Modify results of django queryset

耗尽温柔 提交于 2020-01-03 05:49:07

问题


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?

来源:https://stackoverflow.com/questions/52150694/modify-results-of-django-queryset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!