Ticket #10970: auto_now_patch_1.diff

File auto_now_patch_1.diff, 2.0 KB (added by Cyberj, 5 years ago)

Patch for rev 12847

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

     
    646646
    647647    def pre_save(self, model_instance, add):
    648648        if self.auto_now or (self.auto_now_add and add):
    649             value = datetime.datetime.now()
     649            value = datetime.date.today()
    650650            setattr(model_instance, self.attname, value)
    651651            return value
    652652        else:
     
    733733                except ValueError:
    734734                    raise exceptions.ValidationError(self.error_messages['invalid'])
    735735
     736    def pre_save(self, model_instance, add):
     737        if self.auto_now or (self.auto_now_add and add):
     738            value = datetime.datetime.now()
     739            setattr(model_instance, self.attname, value)
     740            return value
     741        else:
     742            return super(DateTimeField, self).pre_save(model_instance, add)
     743
    736744    def get_prep_value(self, value):
    737745        return self.to_python(value)
    738746
  • 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()
     
    98102>>> Donut.objects.get(id=d2.id).review
    99103u'Outstanding'
    100104
     105# Test for #10970, auto_now_add for DateField should evaluete to datetime.date instance
     106
     107>>> b = RumBaba()
     108>>> b.save()
     109>>> isinstance(b.backed_time, datetime.datetime)
     110True
     111>>> isinstance(b.backed_date, datetime.date)
     112True
     113
    101114"""}
    102115
    103116# Regression test for #8354: the MySQL backend should raise an error if given
Back to Top