I want the current user to log in (request.user) in the save method of models.py. I want to check the role of the user and perform some operations based on his role. I have cited the models.py code below.
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 used as a built-in Schedule model. I want to check the role of the current registered user and update another model based on the user role. Here I need request.user to check the role of the current user. I do not use any forms or templates and fully use django admin. So, is there a way to get request.user in the save models of a method, or check and update values ββin another model in admin.py.
I checked most of these issues in stackoverflow and none of them provided me with the perfect solution. So please provide me an answer to fix this. Thanks in advance.
python django django-models
arulmr Jun 12 '12 at 6:22 2012-06-12 06:22
source share