'Override specific field __get__ on model instance in Django
I'm trying to temporarily monkey-patch the request.user to change its behavior (in my case, I need to warn developers that the field is deprecated or raise an exception)
I have a middleware like this:
def middleware(request: HttpRequest) -> HttpResponse:
if request.user.is_authenticated and request.user.patient is not None:
def _get_patient():
raise SomeAPIError() # or return value with warning
request.user.patient = _get_patient
response = get_response(request)
return response
return middleware
The problem is that, of course, Django doesn't let me to do this so straightforward. I understand that patient property here is a descriptor, and I want to override its __get__ method, but I don't know how.
I also can't just rename the field in Model and create a property in place of old field, because I still need this field in many places, I just want to make it inaccessible via request.user.patient
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
