I am serializing User objects in JSON, and I would like to indicate in the JSON response whether the serialized user is familiar with the user making the request.
I added the to_dict () method to my User model, which does the preprocessing necessary to serialize the object - this would be a nice place to add an attribute indicating friendship, but since User.to_dict () doesnβt have access to the request object, I can not do it.
Doing this in a view is easy, but I don't want to repeat this code in other views. I would like to βupdateβ User objects for User-user-aware objects.
The user model django.contrib.auth has an is_authenticated attribute, which is really a request attribute, not a model - this attribute only makes sense in the context of a specific web request.
As if I should replace request.user with an instance of RequestUser, a new class that accepts the user and the request and adds request-specific attributes. What a clean way to do this?
source share