如何在 Django 模型上定义任何年份的月份范围?

发布于 2024-12-09 15:40:01 字数 283 浏览 0 评论 0原文

我有一个名为 Event 的 Django 模型,它有一个事件日期的日期字段:

class Event(models.Model):
    event_date = models.DateField()

我希望能够在模型上设置一个方法来判断该事件是“春季学期”活动还是“秋季学期”活动“ 事件。

春季学期定义为一月至五月。秋季是八月到十二月。

我的目标是能够在一年的通用事件列表中按任一学期进行过滤。

我将如何编写定义每个学期的方法?

I have a Django model called Event that has a date field for the date of the event:

class Event(models.Model):
    event_date = models.DateField()

I'd like to be able to set a method on the model to tell whether the event is a "spring semester" event or a "fall semester" event.

The spring semester would be defined as January to May. Fall is August to December.

My goal is to be able to filter by either semester in a generic list of events for a year.

How would I go about writing the method defining each semester?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

有深☉意 2024-12-16 15:40:02

一种方法是:

class SpringSemesterEventManager(models.Manager):
    def get_query_set(self):
        return super(SpringSemesterEventManager, self).get_query_set() \
            .filter(is_spring_semester=True)

class FallSemesterEventManager(models.Manager):
    def get_query_set(self):
        return super(FallSemesterEventManager, self).get_query_set() \
            .filter(is_fall_semester=True)

class Event(models.Model):
    event_date = models.DateField()

    @property
    def is_spring_semester(self):
        month = self.event_date.month
        return True if month >= 1 and month <= 5 else False

    @property
    def is_fall_semester(self):
        month = self.event_date.month
        return True if month >= 8 and month <= 12 else False

    objects = models.Manager()
    spring_semester = SpringSemesterEventManager()
    fall_semester = FallSemesterEventManager()

然后在一个视图中你可以这样做:

fall_events = Event.fall_semester.all()

希望对你有帮助。

One approach is:

class SpringSemesterEventManager(models.Manager):
    def get_query_set(self):
        return super(SpringSemesterEventManager, self).get_query_set() \
            .filter(is_spring_semester=True)

class FallSemesterEventManager(models.Manager):
    def get_query_set(self):
        return super(FallSemesterEventManager, self).get_query_set() \
            .filter(is_fall_semester=True)

class Event(models.Model):
    event_date = models.DateField()

    @property
    def is_spring_semester(self):
        month = self.event_date.month
        return True if month >= 1 and month <= 5 else False

    @property
    def is_fall_semester(self):
        month = self.event_date.month
        return True if month >= 8 and month <= 12 else False

    objects = models.Manager()
    spring_semester = SpringSemesterEventManager()
    fall_semester = FallSemesterEventManager()

Then in a view you can do:

fall_events = Event.fall_semester.all()

Hope that helps you out.

泪痕残 2024-12-16 15:40:02

你看过 django 的 Models 方法吗?你可以在这里找到它们

class Event(models.Model):
    event_date = models.DateField()

    def get_season(self):
        import datetime
        """
        Returns the event's specific season
        """
        if 1 <= datetime.date.today().month < 4:
            return "Winter event"

显然上面的代码在不正确,但你知道该怎么做。

Did you have a look at django's Models' methods? You can find them here

class Event(models.Model):
    event_date = models.DateField()

    def get_season(self):
        import datetime
        """
        Returns the event's specific season
        """
        if 1 <= datetime.date.today().month < 4:
            return "Winter event"

Obviously the code above in incorrect, but you know what to do.

似最初 2024-12-16 15:40:02

您可以简单地在日期范围上使用过滤器,使用 __gt(大于)和 __lt(小于)运算符:

Event.objects.filter(event_date__gt=datetime.date(2011, 1, 1), event_date__lt=(datetime.date(2011,6,1))

或者您可以将学期写入数据库,例如您可以覆盖默认的 save() 方法,以便自动完成每当您保存新的或现有的事件时。所以模型是:

class Event(models.Model):
    event_date = models.DateField()
    semester = models.CharField(max_length=10)

def save(self, *args, **kwargs):
    # determine if the even is in the spring or fall
    if self.event_date.month in [1,2,3,4,5]
        self.semester = 'spring'
    elif self.event_date.month in [8,9,10,11,12]
        self.semester = 'fall'
    super(Event, self).save(*args, **kwargs) # Call the "real" save() method.

然后你可以简单地查询 Event.objects.filter(semester = 'spring')

You can simply use filter on a date range, using __gt (greater than) and __lt (less than) operators:

Event.objects.filter(event_date__gt=datetime.date(2011, 1, 1), event_date__lt=(datetime.date(2011,6,1))

Or you can write the semester into the db, you can for instance override the default save() method so this is done automatically whenever you save a new or existing event. So the model would be:

class Event(models.Model):
    event_date = models.DateField()
    semester = models.CharField(max_length=10)

def save(self, *args, **kwargs):
    # determine if the even is in the spring or fall
    if self.event_date.month in [1,2,3,4,5]
        self.semester = 'spring'
    elif self.event_date.month in [8,9,10,11,12]
        self.semester = 'fall'
    super(Event, self).save(*args, **kwargs) # Call the "real" save() method.

Then you can simple query Event.objects.filter(semester = 'spring')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文