Django: Get current user in model save
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 perform some operations based on his role.
models.py:
class TimeSheet(models.Model):
check_in_time = models.TimeField()
check_out_time = models.TimeField()
class Tasks(models.Model):
time_sheet = models.ForeignKey(TimeSheet)
project = models.ForeignKey(Project)
start_time = models.TimeField()
end_time = models.TimeField()
def save(self, *args,**kwargs):
project = SpentTime.objects.get(project__project__id = self.project.id)
start = datetime.datetime.strptime(str(self.start_time), '%H:%M:%S')
end = datetime.datetime.strptime(str(self.end_time), '%H:%M:%S')
time = float("{0:.2f}".format((end - start).seconds/3600.0))
if common.isDesigner(request.user):
SpentTime.objects.filter(project__project__id = self.project.id).update(design = float(project.design) + time)
if common.isDeveloper(request.user):
SpentTime.objects.filter(project__project__id = self.project.id).update(develop = float(project.develop) + time)
super(Tasks, self).save(*args, **kwargs)
Here the Tasks
model is being used as inline in Timesheet
model. I want to check the role of the currently logged in user and update another model based on the user's role. Here I need request.user
to check the role of the current user. I am not using any forms or templates and completely making use of Django admin. Is there any method to get request.user
in the save
method or to check and update the values in another model in admin.py?
You can tackle this problem from another angle. Instead of changing the models save method you should override the AdminSites save_model
method. There you'll have the request object and can access the logged in user data as you already pointed out.
Have a look at this chapter of the docs: Django ModelAdmin documentation save_model
I found a way to do that, it involves declaring a MiddleWare, though. Create a file called get_username.py
inside your app, with this content:
from threading import current_thread
_requests = {}
def get_username():
t = current_thread()
if t not in _requests:
return None
return _requests[t]
class RequestMiddleware(object):
def process_request(self, request):
_requests[current_thread()] = request
Edit your settings.py
and add it to the MIDDLEWARE_CLASSES
:
MIDDLEWARE_CLASSES = (
...
'yourapp.get_username.RequestMiddleware',
)
Now, in your save()
method, you can get the current username like this:
from get_username import get_username
...
def save(self, *args, **kwargs):
req = get_username()
print "Your username is: %s" % (req.user)