[Django]-How to pass request/user to Django model

1👍

This will need overide/modification of models save() method, for instance:

class Exam(models.Model):
    ...
    def save(self, *args, **kwargs):
        request = kwargs.pop('request', None)
        # print(f'user {request.user} saved the model')
        ...
        super().save(*args, **kwargs)

1👍

Try like this, might you can get in kwargs in save method of the model.

def save_model(self, request, obj, form, change):
    obj.save({"request": request})
    super().save_model(request, obj, form, change)

Leave a comment