How to dynamically add parameters for each different request?

Hi there, I want to use request.raw_args to get the parameters of the GET request,
But I found that the attribute was gone
I want to implement this function myself, But Request Class can’t set attribute
What should I do to add attributes to Request or pass parameter to view by request ?
I know I can add attribute for request.app. But think that should be used to store global variables.
can you give me some suggestion?
Please tell me how you regard and deal with adding attributes to each different reuqest
Thank you !
this is my code, but it’s not useful

Blockquote
async def request_check_middleware(request):
if request.method == ‘GET’:
raw_args = {item: request.args.get(item)[0] for item in request.args}
slots = list(request.slots)
slots.append(‘raw_args’)
request.slots = tuple(slots)
request.raw_args = raw_args

Heyo! To add variables to the request, you can use request.ctx

For example:

request.ctx.raw_args = raw_args

This is a fairly new addition so it’s not shown in some of the older examples online. request.ctx is a SimpleNamespace.

1 Like

OK, the method you said solved my problem. Thank you very much.
By the way, what software package do you use to drive the database asynchronously?
I am a novice, I use a small program based on aiomysql written by myself, which is very simple. Do you have any good solutions?
I recently saw the solution of tortoise-orm+aerich+aiomysql. I simply tried it. It can realize functions similar to Django ORM, but tortoise-orm only supports python3.7 or above. I hope to have a solution that supports python3.6 or above.
My English is poor, I am Chinese, so please don’t take it amiss if my English is not standard, thank you!

In the past I used sqlalchemy a lot for it’s ORM, so coming over to an async environment I went with Gino, as it’s built on top of sqlalchemy. There’s a learning curve but the documentation is pretty good and there’s lots of examples all over the internet.

I also like using databases from encode.io, or if you want a higher level abstration, orm (although I have never used orm).

@ahopkins @verdebirth
Wow, That’s great. It’s like opening a door to a new world. Thank you very much. I really like it here very much

2 Likes