Django过滤事件今天发生

Django过滤事件今天发生

本文介绍了Django过滤事件今天发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在Django过滤器中逻辑地表示以下内容。我有一个'event'模型和一个位置模型,可以表示为:

  class Location(models.Model) :
name = models.CharField(max_length = 255)

class事件(models.Model):
start_date = models.DateTimeField()
end_date = models。 DateTimeField()
location = models.ForeignKeyField(Location)

objects = EventManager()

对于给定位置,我想选择今天发生的所有事件。我已经通过EventManager中的bookings_today方法尝试过各种策略,但正确的过滤器语法不包括我:

  class EventManager (models.Manager):
def bookings_today(self,location_id):
bookings = self.filter(location = location_id,start =?end =?)

date()失败,因为这是零的时间,白天的时间对应用程序至关重要,最小和最大的日期,并将其用作书签。此外,还有多种可能的有效配置:

  start_date<今天,end_date在今天
start_date在今天,end_date在今天
start_date在今天,end_date在今天

我需要编写一整套不同的选项,还是有一个更简单和优雅的方法?

解决方案

p>您将需要两个不同的 datetime 阈值 - today_start today_end




$ b今天= datetime.now( ).date()
tomorrow = today + timedelta(1)
today_start = datetime.combine(today,time())
today_end = datetime.combine(tomorrow,time())

今天发生的事情必须在 today_end today_start 之后结束

  class EventManager(models.Manager):
def bookings_today(self,location_id):
#C今天_今天_start如上所述,为了简洁省略
return self.filter(location = location_id,start__lte = today_end,end__gte = today_start)

(PS调用 DateTimeField (而不是一个 DateField )调用 foo_date 令人不快的误导 - 只考虑开始结束 ...)


I'm struggling to logically represent the following in a Django filter. I have an 'event' model, and a location model, which can be represented as:

class Location(models.Model):
    name = models.CharField(max_length=255)

class Event(models.Model):
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    location = models.ForeignKeyField(Location)

    objects = EventManager()

For a given location, I want to select all events occurring today. I've tried various strategies via a 'bookings_today' method in the EventManager, but the right filter syntax eludes me:

class EventManager(models.Manager):
    def bookings_today(self, location_id):
        bookings = self.filter(location=location_id, start=?, end=?)

date() fails as this zeroes out the times, and time during the day is critical to the app, the same goes for min and max of the dates, and using them as bookends. In addition, there are multiple possible valid configurations:

start_date < today, end_date during today
start_date during today, end_date during today
start_date during today, end_date after today

Do I need to code a whole set of different options or is there a more simple and elegant method?

解决方案

You'll need two distinct datetime thresholds - today_start and today_end:

from datetime import datetime, timedelta, time

today = datetime.now().date()
tomorrow = today + timedelta(1)
today_start = datetime.combine(today, time())
today_end = datetime.combine(tomorrow, time())

Anything happening today must have started before today_end and ended after today_start, so:

class EventManager(models.Manager):
    def bookings_today(self, location_id):
        # Construction of today_end / today_start as above, omitted for brevity
        return self.filter(location=location_id, start__lte=today_end, end__gte=today_start)

(P.S. Having a DateTimeField (not a DateField) called foo_date is irritatingly misleading - consider just start and end...)

这篇关于Django过滤事件今天发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:32