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

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I solved this by saving the user in the views, thanks for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.