1

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.

  • Does this answer your question? [Get last\_login time for a certain user? (django)](https://stackoverflow.com/questions/11352495/get-last-login-time-for-a-certain-user-django) – Sirwill98 Jan 28 '21 at 15:15
  • @Sirwill98 Thank you for your comment. If you use this method, `User.objects.get(username="?").last_login` is supposed to solve the problem, but What kind of string should I put in the "?" I don't know how to access the attribute information of the currently logged-in user from `admin.py`. Thank you for your help. – user13209725 Jan 28 '21 at 23:02
  • you can get the current user from request.user, if you are in a form you can add a get_form method that can take the request as a parameter and get the user from that, [this page](https://stackoverflow.com/questions/2864955/django-how-to-get-current-user-in-admin-forms) has more info – Sirwill98 Jan 29 '21 at 07:43
  • @Sirwill98 Thanks for the reply. I have added it to the question box because I cannot write it in this comment box. I hope you can answer my question. Thank you very much. – user13209725 Jan 29 '21 at 10:17

0 Answers0