Django YearArchiveView年度最佳消息?

时间:2017-01-30 22:08:05

标签: django django-models django-views

我正在创建一个简单的日历应用程序,用于跟踪学年(8月至5月)的事件。我想做的是拥有一个存档视图,允许用户选择一个学年,并查看该学年的活动价值。

为此,我想我会使用Django Class YearArchiveView。问题是,我不知道如何过滤结果,以便获得从1月到12月的结果,而不是8月到5月。

查看:

class EventArchiveView(YearArchiveView):
    template_name = "cal/cal_archive.html"
    queryset = CalEvent.objects.all()
    date_field = 'start_date'
    make_object_list = True
    allow_future = True

型号:

class CalEvent(Audit):
    CAL_CHOICES = (
        ('HOLIDAY','Holiday'),
        ('SECONDARY','Secondary'),
        ('ELEMENTARY','Elementary'),
        ('TEACHER','Teacher'),
        ('TERM','Term'),
        ('TESTING','Testing'),
        ('GRAD','Graduation'),
    )
    title = models.CharField(max_length=255)
    category = models.CharField(max_length=20,choices=CAL_CHOICES,default='HOLIDAY')
    start_date = models.DateField(help_text="Format: M/D/Y")
    end_date = models.DateField(blank=True,null=True,help_text="Format: M/D/Y. Not Required. Fill if event will spans multiple days.")
    week = models.IntegerField()
    active = models.BooleanField(default=False)

    def __str__(self):
        return self.title

    def cat_verbose(self):
        return dict(CalEvent.CAL_CHOICES)[self.category]

    class Meta:
        verbose_name_plural = "Cal Events"

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下方法覆盖日期

class EventArchiveView(YearArchiveView):
    template_name = "cal/cal_archive.html"
    queryset = CalEvent.objects.all()
    date_field = "start_date"
    make_object_list = True
    allow_future = True

    def _get_current_year(self, date):
        return date.replace(month=8, day=1)

    def _get_next_year(self, date):
        return date.replace(year=date.year+1, month=5, day=1)