Ticket #17728: 17728.diff

File 17728.diff, 5.5 KB (added by Anssi Kääriäinen, 13 years ago)
  • django/db/models/sql/where.py

    diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py
    index 1455ba6..d215f4c 100644
    a b import datetime  
    88from itertools import repeat
    99
    1010from django.utils import tree
    11 from django.db.models.fields import Field
     11from django.db.models.fields import Field, DateTimeField
    1212from django.db.models.sql.datastructures import EmptyResultSet, FullResultSet
    1313
    1414# Connection types
    class WhereNode(tree.Node):  
    145145            except EmptyShortCircuit:
    146146                raise EmptyResultSet
    147147        else:
     148            # If the value is a datetime value, we need to take care converting it into
     149            # proper db datettime type.
     150            if isinstance(params_or_value, datetime.datetime):
     151                params_or_value = connection.ops.value_to_db_datetime(params_or_value)
    148152            params = Field().get_db_prep_lookup(lookup_type, params_or_value,
    149153                connection=connection, prepared=True)
    150154        if isinstance(lvalue, tuple):
  • tests/modeltests/timezones/models.py

    diff --git a/tests/modeltests/timezones/models.py b/tests/modeltests/timezones/models.py
    index 9296edf..f0cc792 100644
    a b class Event(models.Model):  
    66class MaybeEvent(models.Model):
    77    dt = models.DateTimeField(blank=True, null=True)
    88
     9class Session(models.Model):
     10    name = models.CharField(max_length=20)
     11
     12class SessionEvent(models.Model):
     13    dt = models.DateTimeField()
     14    session = models.ForeignKey(Session, related_name='events')
     15
    916class Timestamp(models.Model):
    1017    created = models.DateTimeField(auto_now_add=True)
    1118    updated = models.DateTimeField(auto_now=True)
  • tests/modeltests/timezones/tests.py

    diff --git a/tests/modeltests/timezones/tests.py b/tests/modeltests/timezones/tests.py
    index dc9cfb3..8184059 100644
    a b from django.utils.tzinfo import FixedOffset  
    2525from django.utils.unittest import skipIf, skipUnless
    2626
    2727from .forms import EventForm, EventSplitForm, EventModelForm
    28 from .models import Event, MaybeEvent, Timestamp
     28from .models import Event, MaybeEvent, Session, SessionEvent, Timestamp
    2929
    3030
    3131# These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time)
    class LegacyDatabaseTests(BaseDateTimeTests):  
    231231            'dt__max': datetime.datetime(2011, 9, 1, 23, 20, 20),
    232232        })
    233233
     234    def test_query_annotation(self):
     235        # Only min and max make sense for datetimes.
     236        morning = Session.objects.create(name='morning')
     237        afternoon = Session.objects.create(name='afternoon')
     238        SessionEvent.objects.create(dt=datetime.datetime(2011, 9, 1, 23, 20, 20), session=afternoon)
     239        SessionEvent.objects.create(dt=datetime.datetime(2011, 9, 1, 13, 20, 30), session=afternoon)
     240        SessionEvent.objects.create(dt=datetime.datetime(2011, 9, 1, 3, 20, 40), session=morning)
     241        morning_min_dt = datetime.datetime(2011, 9, 1, 3, 20, 40)
     242        afternoon_min_dt = datetime.datetime(2011, 9, 1, 13, 20, 30)
     243        self.assertQuerysetEqual(
     244                Session.objects.annotate(dt=Min('events__dt')).order_by('dt'),
     245                [morning_min_dt, afternoon_min_dt],
     246                transform=lambda d: d.dt)
     247        self.assertQuerysetEqual(
     248                Session.objects.annotate(dt=Min('events__dt')).filter(dt__lt=afternoon_min_dt),
     249                [morning_min_dt],
     250                transform=lambda d: d.dt)
     251        self.assertQuerysetEqual(
     252                Session.objects.annotate(dt=Min('events__dt')).filter(dt__gte=afternoon_min_dt),
     253                [afternoon_min_dt],
     254                transform=lambda d: d.dt)
     255
    234256    def test_query_dates(self):
    235257        Event.objects.create(dt=datetime.datetime(2011, 1, 1, 1, 30, 0))
    236258        Event.objects.create(dt=datetime.datetime(2011, 1, 1, 4, 30, 0))
    class NewDatabaseTests(BaseDateTimeTests):  
    412434            'dt__max': datetime.datetime(2011, 9, 1, 23, 20, 20, tzinfo=EAT),
    413435        })
    414436
     437    def test_query_annotation(self):
     438        # Only min and max make sense for datetimes.
     439        morning = Session.objects.create(name='morning')
     440        afternoon = Session.objects.create(name='afternoon')
     441        SessionEvent.objects.create(dt=datetime.datetime(2011, 9, 1, 23, 20, 20, tzinfo=EAT), session=afternoon)
     442        SessionEvent.objects.create(dt=datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT), session=afternoon)
     443        SessionEvent.objects.create(dt=datetime.datetime(2011, 9, 1, 3, 20, 40, tzinfo=EAT), session=morning)
     444        morning_min_dt = datetime.datetime(2011, 9, 1, 3, 20, 40, tzinfo=EAT)
     445        afternoon_min_dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
     446        self.assertQuerysetEqual(
     447                Session.objects.annotate(dt=Min('events__dt')).order_by('dt'),
     448                [morning_min_dt, afternoon_min_dt],
     449                transform=lambda d: d.dt)
     450        self.assertQuerysetEqual(
     451                Session.objects.annotate(dt=Min('events__dt')).filter(dt__lt=afternoon_min_dt),
     452                [morning_min_dt],
     453                transform=lambda d: d.dt)
     454        self.assertQuerysetEqual(
     455                Session.objects.annotate(dt=Min('events__dt')).filter(dt__gte=afternoon_min_dt),
     456                [afternoon_min_dt],
     457                transform=lambda d: d.dt)
     458
    415459    def test_query_dates(self):
    416460        # Same comment as in test_query_date_related_filters.
    417461        Event.objects.create(dt=datetime.datetime(2011, 1, 1, 1, 30, 0, tzinfo=EAT))
Back to Top