'last_updated date and time keeps updating everytime the user logs into the system but doesn't update any of their details in Django
In my model for user profile, the profile_last_updated = models.DateTimeField(auto_now=True) keeps updating every time the relevant profile owner logs in but doesn't update any of their details. Is there a way to fix this? I found this on SO django last updated keeps updating when there is no new information but it's a bit confusing. Appreciate any help - thanks!
views.py
def user_profile_detail_view(request,pk):
try:
myprofile = get_object_or_404(User, id=pk)
except ObjectDoesNotExist:
return redirect('handler404')
Solution 1:[1]
See, the problem with auto_now=True is that the field always gets update whenever the .save() method is called and thus you are seeing an unexpected behaviour. Instead what you can do is you can remove auto_now or set it to False then always manually update the field using DateTime.
Example:
from datetime import datetime as dt
def your_view_function(request):
#your rest of the function for processing user profile
Models.objects.filter(id=id_of_field_updated).update(profile_last_updated=dt.now())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SimbaOG |
