You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
AutoLastModifiedField caches the default value for new entries rather than calling now every time.
Environment
Django Model Utils version: >= 4.0.0
Django version: Tested on 2.2 and 3.0
Python version: 3.8
Code examples
classMyModel(models.Model):
my_field=AutoLastModifiedField()
withfreezegun.freezetime(time1):
instance1=MyModel()
withfreezegun.freezetime(time2):
instance2=MyModel()
assert(instance1.my_field!=instance2.my_field) # this will fail
Explanation
AutoLastModifiedField has a cache for the default value
defget_default(self):
"""Return the default value for this field."""ifnothasattr(self, "_default"):
self._default=self._get_default()
returnself._default
This overrides the base django field get_default by always returning the value stored on the first call of save()
I noticed this while upgrading the version and running tests with various freezed time values. My temporary solution was to remove the cache.
I do not understand the purpose of caching the value, but my suggestion is to remove the caching or to keep the django logic that caches the value if not callable.
defget_default(self):
"""Return the default value for this field."""returnself._get_default()
@cached_propertydef_get_default(self):
ifcallable(self.default):
returnself.defaultreturnlambda: self.default
The text was updated successfully, but these errors were encountered:
nesaro
added a commit
to nesaro/django-model-utils
that referenced
this issue
Mar 18, 2022
Problem
AutoLastModifiedField caches the default value for new entries rather than calling
now
every time.Environment
Code examples
Explanation
AutoLastModifiedField has a cache for the default value
This overrides the base django field get_default by always returning the value stored on the first call of
save()
I noticed this while upgrading the version and running tests with various freezed time values. My temporary solution was to remove the cache.
This bug was introduced in version 4.0.0 1878537
I do not understand the purpose of caching the value, but my suggestion is to remove the caching or to keep the django logic that caches the value if not callable.
The text was updated successfully, but these errors were encountered: