按日期重组时django中的问题

时间:2014-06-06 17:38:11

标签: python django datetime unicode

我将尝试尽可能详细地说明我遇到此问题的背景。

型号:

class Schedule(models.Model):
    id = models.AutoField(primary_key=True)
    project = models.ForeignKey(ProjectSchedule, related_name='schedules')
    person = models.ForeignKey(PersonSchedule, related_name='schedules')
    week_date = models.DateField() # first day of the week
    hours = models.IntegerField(default=0)

    class Meta:
        ordering = ['project']
        unique_together = ('project', 'person', 'week_date')

    def __unicode__(self):
        return unicode("%s from %s schedule (%shs)" % (self.person, self.project, self.hours))

查看:

class ReportView(TemplateView):
    start_year = None
    start_month = None
    start_day = None
    start_week = None

    end_year = None
    end_month = None
    end_day = None
    end_week = None

    def get(self, request, *args, **kwargs):
        try:
            self.start_year = int(kwargs.get('start_year'))
            self.start_month = int(kwargs.get('start_month'))
            self.start_day = int(kwargs.get('start_day'))
            self.start_week = datetime.date(self.start_year, self.start_month, self.start_day)

            self.end_year = int(kwargs.get('end_year'))
            self.end_month = int(kwargs.get('end_month'))
            self.end_day = int(kwargs.get('end_day'))
            self.end_week = datetime.date(self.end_year, self.end_month, self.end_day)

    def get_schedules(self):
        return Schedule.objects.filter(week_date__range=[self.start_week, self.end_week])

    def get_context_data(self, **kwargs):
        context = super(ProjectsReportTableView, self).get_context_data(**kwargs)
        context.update({'schedules': self.get_schedules()})
        return context

模板:

<h2>Schedules by week</h2>

{% regroup schedules by week_date as schedules_grouped_by_week %}
{% for week_schedules_group in schedules_grouped_by_week %}
    <h3>{{ week_schedules_group.grouper }}</h3>
    <p>{{ week_schedules_group.list }}</p>
{% endfor %}
<br/>

<h3>All schedules regrouped</h3>
<p>{{ schedules_grouped_by_week }}</p>

结果:

Schedules by week

June 23, 2014 <------ SAME
[<Schedule: Kelian from Oxford schedule (1hs)>]

June 2, 2014
[<Schedule: [Bad Unicode data]>, <Schedule: [Bad Unicode data]>]

June 23, 2014 <------ SAME
[<Schedule: Rodrigo from Influester schedule (56hs)>]

June 9, 2014
[<Schedule: [Bad Unicode data]>]


All schedules regrouped     
[{u'list': [<Schedule: Kelian from Oxford schedule (1hs)>], u'grouper': datetime.date(2014, 6, 23)}, <---SAME
 {u'list': [<Schedule: [Bad Unicode data]>, <Schedule: [Bad Unicode data]>], u'grouper': datetime.date(2014, 6, 2)},
 {u'list': [<Schedule: Rodrigo from Influester schedule (56hs)>], u'grouper': datetime.date(2014, 6, 23)}, <---SAME
 {u'list': [<Schedule: [Bad Unicode data]>], u'grouper': datetime.date(2014, 6, 9)}]

注意:

糟糕的unicode事情是因为我的母语(西班牙语)的特殊字符。或者有一个问题是由datetime.date()对象重新组合,或者在某些unicode /重组事件周围存在问题(请注意,那些带有“错误Unicode数据”的计划被正确分组,现在不是为什么)。我发现很少有人在论坛/堆栈溢出/谷歌群体中遇到这个问题,但他们正在进行分组,但是他们的模型将日期时间对象(date = models.DateTimeField())作为日期,因此每个具有秒数差异的日期会分别进行分组,通过date.date分组修复了他们的问题,但这不是我的情况,我正在处理代码中的datetime.date()对象,如你所见。

我正在使用django 1.6.5和python 2.7.3。

1 个答案:

答案 0 :(得分:2)

来自docs

  

请注意,{% regroup %}不会对其输入进行排序。

(从这个意义上说,它就像uniqitertools.groupby。)

如果您订购了get_schedules的结果,您应该得到正确的结果:

def get_schedules(self):
    schedules = Schedule.objects.filter(week_date__range=[self.start_week, self.end_week])
    schedules = schedules.order_by('week_date')
    return schedules