Ticket #10970: 10970.diff

File 10970.diff, 2.6 KB (added by Ramiro Morales, 14 years ago)

Patch updated to r14002 (test fixed to actually fail before the fix and updated to unittest)

  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    a b  
    622622
    623623    def pre_save(self, model_instance, add):
    624624        if self.auto_now or (self.auto_now_add and add):
    625             value = datetime.datetime.now()
     625            value = datetime.date.today()
    626626            setattr(model_instance, self.attname, value)
    627627            return value
    628628        else:
     
    709709                except ValueError:
    710710                    raise exceptions.ValidationError(self.error_messages['invalid'])
    711711
     712    def pre_save(self, model_instance, add):
     713        if self.auto_now or (self.auto_now_add and add):
     714            value = datetime.datetime.now()
     715            setattr(model_instance, self.attname, value)
     716            return value
     717        else:
     718            return super(DateTimeField, self).pre_save(model_instance, add)
     719
    712720    def get_prep_value(self, value):
    713721        return self.to_python(value)
    714722
  • tests/regressiontests/datatypes/models.py

    diff --git a/tests/regressiontests/datatypes/models.py b/tests/regressiontests/datatypes/models.py
    a b  
    1919
    2020    def __str__(self):
    2121        return self.name
     22
     23class RumBaba(models.Model):
     24    baked_date = models.DateField(auto_now_add=True)
     25    baked_timestamp = models.DateTimeField(auto_now_add=True)
  • tests/regressiontests/datatypes/tests.py

    diff --git a/tests/regressiontests/datatypes/tests.py b/tests/regressiontests/datatypes/tests.py
    a b  
    33from django.test import TestCase
    44from django.utils import tzinfo
    55
    6 from models import Donut
     6from models import Donut, RumBaba
    77from django.conf import settings
    88
    99class DataTypesTestCase(TestCase):
     
    8181            d = Donut(name='Bear claw', consumed_at=dt)
    8282            self.assertRaises(ValueError, d.save)
    8383            # ValueError: MySQL backend does not support timezone-aware datetimes.
     84
     85    def test_datefield_auto_now_add(self):
     86        """Test for #10970, auto_now_add for DateField should store a Python datetime.date, not a datetime.datetime"""
     87        b = RumBaba.objects.create()
     88        # Verify we didn't break DateTimeField behavior
     89        self.assert_(isinstance(b.baked_timestamp, datetime.datetime))
     90        # We need to test this this way because datetime.datetime inherits
     91        # from datetime.date:
     92        self.assert_(isinstance(b.baked_date, datetime.date) and not isinstance(b.baked_date, datetime.datetime))
Back to Top