Ticket #10970: auto_now_patch.diff

File auto_now_patch.diff, 2.1 KB (added by summerisgone, 6 years ago)
  • django/db/models/fields/__init__.py

     
    485485
    486486    def pre_save(self, model_instance, add):
    487487        if self.auto_now or (self.auto_now_add and add):
    488             value = datetime.datetime.now()
     488            value = datetime.date.today()
    489489            setattr(model_instance, self.attname, value)
    490490            return value
    491491        else:
     
    564564                    raise exceptions.ValidationError(
    565565                        _('Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.'))
    566566
     567    def pre_save(self, model_instance, add):
     568        if self.auto_now or (self.auto_now_add and add):
     569            value = datetime.datetime.now()
     570            setattr(model_instance, self.attname, value)
     571            return value
     572        else:
     573            return super(DateTimeField, self).pre_save(model_instance, add)
     574
    567575    def get_db_prep_value(self, value):
    568576        # Casts dates into the format expected by the backend
    569577        return connection.ops.value_to_db_datetime(self.to_python(value))
  • tests/regressiontests/datatypes/models.py

     
    2121    def __str__(self):
    2222        return self.name
    2323
     24class RumBaba(models.Model):
     25    backed_date = models.DateField(auto_now_add=True)
     26    backed_time = models.DateTimeField(auto_now_add=True)
     27
    2428__test__ = {'API_TESTS': """
    2529# No donuts are in the system yet.
    2630>>> Donut.objects.all()
     
    8993>>> Donut.objects.get(id=d2.id).review
    9094u'Outstanding'
    9195
     96# Test for #10970, auto_now_add for DateField should evaluete to datetime.date instance
     97
     98>>> b = RumBaba()
     99>>> b.save()
     100>>> isinstance(b.backed_time, datetime.datetime)
     101True
     102>>> isinstance(b.backed_date, datetime.date)
     103True
     104
    92105"""}
    93106
    94107# Regression test for #8354: the MySQL backend should raise an error if given
Back to Top