Django: Get current user in model save

后端 未结 6 1607
既然无缘
既然无缘 2020-12-04 23:29

I want to get the currently logged in user (request.user) in the save method of models.py. I want to check the role of the user and see if it can p

6条回答
  •  青春惊慌失措
    2020-12-05 00:00

    Have you tried using the following library:

    https://pypi.org/project/django-currentuser/

    Snippet of how it is used from the website:

    from django_currentuser.middleware import (get_current_user, get_current_authenticated_user)
    # As model field:
    from django_currentuser.db.models import CurrentUserField
    class Foo(models.Model):
        created_by = CurrentUserField()
    

提交回复
热议问题