From 279fc8599b72d56729871f241d6e91dc2d409158 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Thu, 26 Jun 2008 07:51:19 +0000 Subject: [PATCH] Fixed #7105 -- Fixed dates() queries in light of model inheritance. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7763 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/query.py | 2 +- django/db/models/sql/subqueries.py | 8 ++++--- .../model_inheritance_regress/models.py | 22 +++++++++++++++---- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 2e9752288e..eff4c57a7c 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -688,7 +688,7 @@ class DateQuerySet(QuerySet): """ self.query = self.query.clone(klass=sql.DateQuery, setup=True) self.query.select = [] - self.query.add_date_select(self._field.column, self._kind, self._order) + self.query.add_date_select(self._field, self._kind, self._order) if self._field.null: self.query.add_filter(('%s__isnull' % self._field.name, False)) diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py index 28436abede..0bb741d706 100644 --- a/django/db/models/sql/subqueries.py +++ b/django/db/models/sql/subqueries.py @@ -357,12 +357,14 @@ class DateQuery(Query): date = typecast_timestamp(str(date)) yield date - def add_date_select(self, column, lookup_type, order='ASC'): + def add_date_select(self, field, lookup_type, order='ASC'): """ Converts the query into a date extraction query. """ - alias = self.join((None, self.model._meta.db_table, None, None)) - select = Date((alias, column), lookup_type, + result = self.setup_joins([field.name], self.get_meta(), + self.get_initial_alias(), False) + alias = result[3][-1] + select = Date((alias, field.column), lookup_type, self.connection.ops.date_trunc_sql) self.select = [select] self.select_fields = [None] diff --git a/tests/regressiontests/model_inheritance_regress/models.py b/tests/regressiontests/model_inheritance_regress/models.py index 8801715a0c..33e2e0e4f6 100644 --- a/tests/regressiontests/model_inheritance_regress/models.py +++ b/tests/regressiontests/model_inheritance_regress/models.py @@ -2,6 +2,8 @@ Regression tests for Model inheritance behaviour. """ +import datetime + from django.db import models class Place(models.Model): @@ -10,7 +12,7 @@ class Place(models.Model): class Meta: ordering = ('name',) - + def __unicode__(self): return u"%s the place" % self.name @@ -35,11 +37,17 @@ class ParkingLot(Place): def __unicode__(self): return u"%s the parking lot" % self.name +class Parent(models.Model): + created = models.DateTimeField(default=datetime.datetime.now) + +class Child(Parent): + name = models.CharField(max_length=10) + __test__ = {'API_TESTS':""" # Regression for #7350, #7202 -# Check that when you create a Parent object with a specific reference to an existent -# child instance, saving the Parent doesn't duplicate the child. -# This behaviour is only activated during a raw save - it is mostly relevant to +# Check that when you create a Parent object with a specific reference to an +# existent child instance, saving the Parent doesn't duplicate the child. This +# behaviour is only activated during a raw save - it is mostly relevant to # deserialization, but any sort of CORBA style 'narrow()' API would require a # similar approach. @@ -117,4 +125,10 @@ __test__ = {'API_TESTS':""" >>> [sorted(d.items()) for d in dicts] [[('name', u"Guido's All New House of Pasta"), ('serves_gnocchi', False), ('serves_hot_dogs', False)]] +# Regressions tests for #7105: dates() queries should be able to use fields +# from the parent model as easily as the child. +>>> obj = Child.objects.create(name='child', created=datetime.datetime(2008, 6, 26, 17, 0, 0)) +>>> Child.objects.dates('created', 'month') +[datetime.datetime(2008, 6, 1, 0, 0)] + """}