How to get currently logged in user for model inline in django 1.6.5 with foreignkey to the same user table more than once?

Similar to this problem cited here Django: Get current user in model save , but my model is different i.e Parent model has the foreign key to different user types pointing to the same django auth user table. The Inline table is a remarks table which, “if changed” needs to capture the currently logged in username. In short, need to know exactly who has changed the remarks. I am able to display the changed remarks in the inline model, but don’t know how to get the user.Models:

class Task(models.Model):
    description = models.CharField(max_length=40,verbose_name='Task')
    requestor = models.ForeignKey(User,related_name='requestor')
    assigned_by = models.ForeignKey(User,null=True,blank=True,
    assigned_to = models.ForeignKey(User,null=True,blank=True,
    update = models.TextField(max_length=300,blank=True,null=True,
                               default='No update')
    def __unicode__(self):
        return self.description

    def update_history(self):
        return UpdateHistory.objects.filter(task=self).order_by('-version')

   def save(self, *args, **kwargs):
        super(Task, self).save(*args, **kwargs)
        # save update history
        update_history = self.update_history()
        if not update_history or self.update != update_history[0].text:
            newUpdate = UpdateHistory(task=self,text=self.update,

class UpdateHistory(models.Model):
    version = models.IntegerField(editable=False)
    task = models.ForeignKey(Task, related_name='updates_history')
    text = models.CharField(max_length=500, verbose_name="update")
    date = models.DateTimeField(
    user = models.ForeignKey(User,blank=True,null=True)

    class Meta:
        unique_together = ('version', 'task',)
        ordering = ('-version',)
        verbose_name_plural = 'Update history(latest on the top)'

    def save(self, *args, **kwargs):
        # start with version 1 and increment it for each comment
        current_version = UpdateHistory.objects.filter(
        self.version = current_version[0].version + 1 if current_version else 1 =
        #TODO: get the currently logged in user and save
        # self.user = User.objects.get(
        super(UpdateHistory, self).save(*args, **kwargs)

The user changing the update need not always be the requestor(which is happening in the below code). How to go about. I wish to use only the django admin interface.

Source: linux

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.