在Django中查找特定范围内的重叠日期

时间:2019-12-30 16:10:02

标签: django python-3.x

我在Django中找到重叠的日期范围时遇到问题。我有两个模型client.on("channelDelete", async function(channel, oldRole, newRole) { ////////// if (channel.guild.id !== "629748522249355295") return; //////////BU SUNUCUDA İSE const bilgilendir = await channel.guild .fetchAuditLogs({ type: "CHANNEL_DELETE" }) .then(hatırla => hatırla.entries.first()); let yapanad = bilgilendir.executor; let idler = bilgilendir.executor.id; if (idler === "222222") return; ////// BU KİŞİ YAPARSA SAL BUNU let logs = await channel.guild.fetchAuditLogs({ type: "CHANNEL_DELETE" }); ////////// if (logs.entries.first().executor.bot) return; channel.guild .member(logs.entries.first().executor) .roles.filter(role => role.name !== "@everyone") .array() .forEach(role => { ////////// channel.guild .member(logs.entries.first().executor) .roles.forEach(roller => channel.guild .member(logs.entries.first().executor) .removeRole(channel.guild.roles.get(roller.id)) ); }); const sChannel = channel.guild.channels.find( c => c.id === "641032067840344064" ); ////////MESAJİJ GİDECEĞİ KANAL İD const cıks = new Discord.RichEmbed() .setColor("BLACK") .setTimestamp() .setDescription( `**${channel.name} adlı Kanal Silindi.\nSilen kişi: ${yapanad}\nBu Kişinin Rollerinin Tümünü Aldım.**` ) .setFooter("Created by Tokuchi."); /// sChannel.send(cıks); channel.guild.owner.send(cıks); }); ,它们的日期范围:

reunion

还有class reunion(models.Model): resource = models.ForeignKey(resource, on_delete=models.CASCADE) start = models.DateTimeField() end = models.DateTimeField() title = models.CharField(max_length=100) 模型:

resource

当我需要在特定日期范围内进行新的聚会时,我需要找到一个不重叠的资源,所以我使用这种方法:

class resource (models.Model):
    site = models.ForeignKey(site, on_delete=models.CASCADE)
    name = models.CharField(max_length=60)    
    def isAvaible(self, endDate, initialDate):
        try:
            self.reunion_set.get(Q(start__lt=endDate) | Q(end__gt=initialDate))  
            return False
        except:
            return True

但我的代码显示日期范围:(12/30/2019 11:00-12/30/2019 12:00)与(12/31/2019 11:30-12/31/2019 12: 30)好像只是比较时间而不是日期。我一直在搜索很多东西,但是没有运气。

我的错误在哪里?

我将日期作为字符串获取,并使用def getAvaibleAccount(initialDate, endDate): avaibleResources = resource.objects.all() for avaibleResource in avaibleResources: if avaibleResource.isAvaible(initialDate,endDate): return avaibleResource return None 进行解析。

1 个答案:

答案 0 :(得分:2)

两个间隔(s 1 ,t 1 (s 2 ,t < sub> 2 )给出 t 1 2 t 2 1 。因此,这意味着给定 t 1 ≥s 2 t 2 ≥s <的两个资源重叠sub> 1

因此,这意味着您应该执行如下检查:

def isAvaible(self, endDate, initialDate):
    return not self.reunion_set.filter(end__gte=intialDate, start__lte=endDate).exists()

请注意,您不应在此处 使用.get(..) [Django-doc],因为当没有 记录或存在多个记录时,这将引发错误。 记录(两个或更多)。您可以在这里使用.exists() [Django-doc]来查找是否存在这样的记录。