2

I have a model Package with a field owner that should contain the user ID that has created the object instance. I thought about overriding the save() method but I didn't figure out how to get the logged in user. I have proceeded this way

class Package(models.Model):
    source = models.CharField(max_length=20)
    destination = models.CharField(max_length=20)
    date_estimation = models.DateTimeField()
    owner = models.ForeignKey('auth.User', related_name='packages', on_delete=models.CASCADE)

    def save(self, *args, **kwargs):
    #WAHT TO DO HERE ?
        super(Package, self).save(*args, **kwargs)

How can I save the current logged in user ? Thank you

Akamee
  • 593
  • 1
  • 6
  • 21
  • 1
    Check out this link and see if it helps you https://stackoverflow.com/questions/1477319/in-django-how-do-i-know-the-currently-logged-in-user – Michael Platt Jul 24 '17 at 15:21

1 Answers1

4

Is recommended save the user in the views with Package.owner = request.User but if you want to use it in save method read this

Mauricio Cortazar
  • 4,049
  • 2
  • 17
  • 27