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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With