Ticket #17508: t17508_1.diff

File t17508_1.diff, 2.5 KB (added by hirokiky, 2 years ago)

Patch to accept less specific dates for DateDetailView.

  • django/views/generic/dates.py

    diff --git a/django/views/generic/dates.py b/django/views/generic/dates.py
    index 9ffaca4..1637670 100644
    a b class BaseDateDetailView(YearMixin, MonthMixin, DayMixin, DateMixin, BaseDetailV 
    628628    Detail view of a single object on a single date; this differs from the
    629629    standard DetailView by accepting a year/month/day in the URL.
    630630    """
     631    date_separate_unity = "daily"
     632
    631633    def get_object(self, queryset=None):
    632634        """
    633635        Get the object this request displays.
    634636        """
    635         year = self.get_year()
    636         month = self.get_month()
    637         day = self.get_day()
    638         date = _date_from_string(year, self.get_year_format(),
    639                                  month, self.get_month_format(),
    640                                  day, self.get_day_format())
     637        date_field= self.get_date_field()
     638
     639        if self.date_separate_unity == "yearly":
     640            date = _date_from_string(self.get_year(), self.get_year_format())
     641            get_until_date = self._get_next_year
     642        elif self.date_separate_unity == "monthly":
     643            date = _date_from_string(self.get_year(), self.get_year_format(),
     644                                     self.get_month(), self.get_month_format())
     645            get_until_date = self._get_next_month
     646        elif self.date_separate_unity == "daily":
     647            date = _date_from_string(self.get_year(), self.get_year_format(),
     648                                     self.get_month(), self.get_month_format(),
     649                                     self.get_day(), self.get_day_format())
     650            get_until_date = self._get_next_day
     651        else:
     652            raise ValueError("Unknown date unit.")
    641653
    642654        # Use a custom queryset if provided
    643655        qs = queryset or self.get_queryset()
    class BaseDateDetailView(YearMixin, MonthMixin, DayMixin, DateMixin, BaseDetailV 
    651663        # Filter down a queryset from self.queryset using the date from the
    652664        # URL. This'll get passed as the queryset to DetailView.get_object,
    653665        # which'll handle the 404
    654         lookup_kwargs = self._make_single_date_lookup(date)
     666        since = self._make_date_lookup_arg(date)
     667        until = self._make_date_lookup_arg(get_until_date(date))
     668
     669        lookup_kwargs = {
     670            '%s__gte' % date_field: since,
     671            '%s__lt' % date_field: until,
     672        }
     673
    655674        qs = qs.filter(**lookup_kwargs)
    656675
    657676        return super(BaseDetailView, self).get_object(queryset=qs)
Back to Top