I have a question.
I'm having trouble figuring out how to get the last login date and time for a logged-in user in the Django admin.
Purpose:
When displaying a list of models, I would like to add a new display item Attention to compare the last login date and time of the logged-in user with the creation date and time of the data.
Then, if the login date is older than the date when the data was created, I want to display something like You haven't done anything since you logged in.
How should I code this?
## app/admin.py
@admin.register(Applicant)
class AppAdmin(admin.ModelAdmin)
list_display = (
'attention', ## <- I don't know how to get it.
'name',
'age',
'updated_at',
'created_at',
)
....
def attention(self, instance):
created_at = instance.created_at
## current_logged_in_user_last_login <- ERROR
if created_at > current_logged_in_user_last_login:
return True
return False
I can solve this problem if I can get the login date and time of the Current User (the logged-in admin user), but what approach should I take to solve this problem?
Thank you very much for reading this far.
Please let me know.
@Sirwill98
Thanks for the reply. I can't write in the comments section, so I wrote it here. It is not intended to be used with Form. I just want to use it to compare dates. I will post the way I have achieved this in my own way. I am not sure if this method is good in general or not, so if you have any advice on the security aspects or a better way, I would appreciate it.
## app/admin.py
@admin.register(Applicant)
class AppAdmin(admin.ModelAdmin)
list_display = (
'attention',
'name',
'age',
'updated_at',
'created_at',
)
def get_queryset(self, request):
"""Private Member Valuables ## This is a particular point of concern."""
self.__administrator_last_login = request.user.last_login
"""Query Set"""
queryset = super().get_queryset(request)
if request.user.is_superuser:
return queryset
def attention(self, instance):
created_at = instance.created_at
administrator_last_login = self.__administrator_last_login
if created_at > administrator_last_login:
return True
return False
Best regard.