HttpRequest instance add attribute dynamically in Django?
NickName:chyoo CHENG Ask DateTime:2016-06-02T16:05:45

HttpRequest instance add attribute dynamically in Django?

I'm confused with HttpRequest object in Django. I know that the AuthenticationMiddleware will add a user to request which is an instance of HttpRequest. And the code is here, but what I want to show is as follow:

request.user = SimpleLazyObject(lambda: get_user(request))

I have read the code of HttpRequest object and can not find a user attribute and its code don't have a __setattr__ method. So I'm curious about why the code do not raise an AtrributeError when access to a no existing attribute.

Thanks for giving help.

Copyright Notice:Content Author:「chyoo CHENG」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37586062/httprequest-instance-add-attribute-dynamically-in-django

More about “HttpRequest instance add attribute dynamically in Django?” related questions

HttpRequest instance add attribute dynamically in Django?

I'm confused with HttpRequest object in Django. I know that the AuthenticationMiddleware will add a user to request which is an instance of HttpRequest. And the code is here, but what I want to sho...

Show Detail

Referencing Current Django HttpRequest Instance

I'm new to python/django and wondering if there is a way to reference the current HttpRequest instance other than passing around the request instance. For example, in .net I could reference the re...

Show Detail

Which function in django creates a HttpRequest instance and hands to a view?

Am try to understand the guts of django, and i cant get any good tutorial on this. I know django views receive a HttpRequest instance as one of the arguments when they are called, what i would l...

Show Detail

django - HttpRequest object has no attribute 'session'

I can't seem to get sessions working. Django complains that HttpRequest objects have no attribute called 'session'. In the documentation it clearly states that if you have the middleware enabled, a...

Show Detail

Djangdo 'HttpRequest' object has no attribute '_stream'

I create an instance of HttpRequest to test my restful "PUT" method. In oder to set the request body like {"active":true}, I write: from django.http import HttpRequest request=HttpRequest() requ...

Show Detail

Django HttpRequest type hint / annotation problem

def my_func(request: HttpRequest) -> str: return request.user.email gives me a warning in PyCharm, saying that user is not a defined attribute. However writing it as request: HttpRequest() re...

Show Detail

How to turn a tornado HTTPRequest into a Django HttpRequest or WSGIRequest?

I'm writing a socket.io app using Django and TornadIO2/Tornado. In the TonradIO2 Session, I have access to Tornado's HTTPRequest objects. However, I have no access to a django request object in my ...

Show Detail

How django add objects attribute to model classes?

As in the Django documentation By default, Django adds a Manager with the name objects to every Django model class How does Django add an attribute to model classes? Whether it is inherited fro...

Show Detail

Django - TDD: 'HttpRequest' has no attribute 'POST'

So I'm doing TDD with Django and I'm stuck on the following problem. Test method for class from TestCase def test_home_page_can_save_POST_request(self): request = HttpRequest request.meth...

Show Detail

Django - AttributeError: type object 'HttpRequest' has no attribute 'GET'

I am trying to get input from a checkbox from a website to go through django. I have already been through stackoverflow and found someone doing the same thing but for some reason it doesn't work fo...

Show Detail