Ticket #13935: 13935.diff

File 13935.diff, 2.5 KB (added by Valentin Golev, 14 years ago)

patch

  • db/models/sql/subqueries.py

     
    188188        """
    189189        Converts the query into a date extraction query.
    190190        """
    191         result = self.setup_joins([field.name], self.get_meta(),
    192                 self.get_initial_alias(), False)
     191        from django.db.models.fields import DateField
     192
     193        if isinstance(field, str):
     194            parts = field.split(LOOKUP_SEP)
     195        else:
     196            parts = [field.name]
     197
     198        try:
     199            result = self.setup_joins(parts, self.get_meta(),
     200                        self.get_initial_alias(), False)
     201        except FieldError:
     202            from django.db.models.fields import FieldDoesNotExist
     203
     204            raise FieldDoesNotExist('%s has no field named %r' % (self.get_meta().object_name, parts[0]))
     205
     206        field = result[0]
     207        assert isinstance(field, DateField), "%r isn't a DateField." \
     208                    % field.name
     209
    193210        alias = result[3][-1]
    194211        select = Date((alias, field.column), lookup_type)
    195212        self.select = [select]
     
    199216        self.distinct = True
    200217        self.order_by = order == 'ASC' and [1] or [-1]
    201218
     219        if field.null:
     220            self.add_filter(('%s__isnull' % field.name, False))
     221
    202222class AggregateQuery(Query):
    203223    """
    204224    An AggregateQuery takes another query as a parameter to the FROM
  • db/models/query.py

     
    988988        self.query.clear_deferred_loading()
    989989        self.query = self.query.clone(klass=sql.DateQuery, setup=True)
    990990        self.query.select = []
    991         field = self.model._meta.get_field(self._field_name, many_to_many=False)
    992         assert isinstance(field, DateField), "%r isn't a DateField." \
    993                 % field.name
    994         self.query.add_date_select(field, self._kind, self._order)
    995         if field.null:
    996             self.query.add_filter(('%s__isnull' % field.name, False))
    997991
     992        # all logic is gone there, because we need to use query.setup_joins
     993        # to guess the real field from something like "article__pub_date"
     994        self.query.add_date_select(self._field_name, self._kind, self._order)
     995
    998996    def _clone(self, klass=None, setup=False, **kwargs):
    999997        c = super(DateQuerySet, self)._clone(klass, False, **kwargs)
    1000998        c._field_name = self._field_name
Back to Top