定期创建插槽 [英] Creation of slots at regular intervals

查看:33
本文介绍了定期创建插槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据一些歌曲创建时隙.例如,如果s如果我的播放列表中有5首歌曲,那么在此示例中,我们将根据逻辑创建插槽的时间间隔,即60/5 = 12分钟,因为此处的歌曲数为5.

I need to create time-slots according to a number of songs. For example, s if my playlist has 5 songs then we create the time-interval of slots by the logic, 60/5 = 12 minutes in this example as the number of songs was 5 here.

我还提供了开始时间和结束时间.在这里,如果我提供 start_time = 5:00 am和end_time = 6:00 am,则总歌曲数= 5

I also provide the start_time and end_time. Here if I provide start_time = 5:00 am and end_time = 6:00 am, total songs = 5

S1: 5:00 - 5:12
S2: 5:12 - 5:24
S3: 5:24 - 5:36
S4: 5:36 - 5:48
S5: 5:48 - 6:00

模型包含以下三个字段:

The Model consists of these three fields:

class SlotCreate(models.Model):
  from_time = models.TimeField(max_length=255, blank=True, null=True)
  to_time = models.TimeField(max_length=255, blank=True, null=True)
  songs = models.ManyToManyField('Song')
  labelling = models.ForeignKey('Labelling',blank=True, null=True)

我正在通过以下查询检查该插槽是否存在:

I am checking whether the slot exists with the following query:

SlotCreate.objects.filter(from_time=from_time,to_time=to_time,labelling=label).exists():
       errors.append("The Slot already exists "+"\n")

这里的问题是用户是否提供:

The problem here is if the user provides:

Start_time = 5:00
End_time = 6:00
Total songs = 3

然后,由于S1,S2,S3,S4,S5已经存在,因此在上述给定的时间间隔中已经存在这些时隙.我在上面检查过的代码无法确定这种情况为

Then the slots already exist in the time interval given above as S1, S2, S3, S4, S5 are already there. The piece of code with which I check above fails to determine this case as

S1: 5:00 - 5:20
S1: 5:20 - 5:40
S1: 5:40 - 6:00

有人可以帮助我在这种情况下想念我什么吗?

Can anyone help me what am I missing here in the condition?

更新1:

我写了以下查询,但是,午夜之后无法锻炼数值.有人可以在下面的查询中提供一些帮助吗,以便我们可以检查插槽是否不重叠并在重叠时引发错误.

I have written the following query, but however, am unable to workout for values after mid-night. Can some-one please offer some help with below query so, that we can check whether the slots do not overlap and raise an error when overlapping.

SlotCreate.objects.filter(labelling = label).exclude((Q(from_time__gte=dateTimeA.time()) & Q(from_time__gte = dateTimeB.time())) | (Q(to_time__lte = dateTimeA.time()) & Q(to_time__lte = dateTimeB.time()))):

推荐答案

我认为您正在寻找类似的东西:

I think you're looking for something like this:

timeA = dateTimeA.time()
timeB = dateTimeB.time()

label_filter = Q(labelling=label)
if timeA <= timeB:
    # Is either from_time OR to_time in the range?
    time_filter = Q(from_time__gte=timeA, from_time__lte=timeB)
    time_filter |= Q(to_time__gte=timeA, to_time__lte=timeB)
else:
    # Crosses midnight
    time_filter = (Q(from_time__gte=timeA) | Q(from_time__lte=timeB))
    time_filter |= (Q(to_time__gte=timeA) | Q(to_time__lte=timeB))

exists = SlotCreate.objects.filter(label_filter & time_filter).exists()

对于午夜条件,您基本上只是交换要比较的值.默认情况下, Q 对象执行 AND ,因此您不需要额外的Q对象就可以在第一种情况下将其拉出.

For the midnight condition, you basically just swap the values you're comparing against. The Q object does an AND by default so you don't need extra Q objects to pull it off in the first condition.

仅需注意,这显然不能处理大于24小时的范围,但是如果仅存储时间值,则您的模型不支持该范围.

Just a note, this obviously doesn't handle ranges larger than 24 hours, but your model doesn't support that if it's only storing time values.

这篇关于定期创建插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆