0

I am working on a project using django-rest-framework 2.6 (we are planning to update to a newer version of the code, but that is not an option at this moment in time). We have many models and a request has come in to log when data is changed. In the module that is dealing with this, I am subscribing to changes in the models we want to check, however the one bit of data that I lack is the user that made the request.

I think I can adjust all the models to have a non database field that contains the user-id, and then write in each of the serialisers to populate it from the request, but I worry about repeating code in all of those places, and someone fixing a bug in one place but not other and it ending up a mess.

Is there a more elegant way to do this?

James Hardy
  • 490
  • 4
  • 14
  • can u post some code ? – levi Nov 02 '15 at 14:09
  • not easily I'm afraid, it is closed source project. But it is a fairly standard DRF site. – James Hardy Nov 02 '15 at 15:34
  • 1
    the package [django-contrib-requestprovider](https://pypi.python.org/pypi/django-contrib-requestprovider/1.0.1), could help you out, maybe – Pynchia Nov 02 '15 at 20:49
  • Yep, django-contrib-requestprovider did it (though a bit of a pain to install via pip). It's better than my attempted approach of passing the data through from the serialiser, as it now will be there even when models are updated indirectly too. Please put this as an answer so that I can approve it and give you the credit – James Hardy Nov 05 '15 at 13:09
  • Actually it's not working for me after all. It shows up when using the Rest Framework UI, but when using it directly with token auth it always returns an AnonymousUser rather than the custom user class that we are using – James Hardy Nov 13 '15 at 12:20

0 Answers0