Question

Simple use case: After a user updates a record, I want to get the changed fields and save them in a history table. I'm using django-ditryfields to grab this history. So my thought process was to use the pre_save signal to grab all the 'dirty' fields and them store them in my history table.

Problem there is that I can't get request.user while using signals. I need this to see which user has made the change to the record. My other thought was just to override the save method of my model but then I also can't get request.user from a model directly either. I would have to send a **kwarg['user'] with the user info from the view to get this info. This is fine but I am going to be making save calls from a bunch of different places around the code. I don't want to have to keep passing request.user every time I edit an object. This is why I'd love to have one spot, like a signal, to handle all of this. Perhaps some middleware I'm not familiar with?

Is there a better way to achieve such a thing?

Was it helpful?

Solution

You cannot access the user object from a signal. You can consider using this third party package: django-requestprovider to access the request object in the signal.

The other way would be to overriding the models' save method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top