0

Given an url like:

http://127.0.0.1:8000/admin/foo/bar/

How can I access the bar variable from the request, using the request.resolver_match.kwargs ?

What kwarg does the admin give too bar?

Tried different keys like class, content_type, slug, model, but none of them work.

EDIT:

It seems the correct kwarg is model_name which is returning None, however. I managed to extract the model name using:

request.resolver_match.url_name.split('_')[1]

But wondering if there is a more direct way.

1 Answer 1

1

That should be just the app label. If you're hitting that url, then you are in the ModelAdminclass. To access the app label (foo in the question example) from ModelAdmin you can do self.model._meta.app_label, so you shouldn't need to go through the request.

You can access the model (bar in question the example) in ModelAdmin with self.model.

The model name won't be directly in the request object unless you put it there. You can get it from the request.path.strip('/').split('/')[-1] or from request.resolver_match.url_name.split('_')[1]

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry...I will edit the question. It is not the app_label, I want the model inside the app_label
@drec4s Then it's just self.model
In your example foo is the app label, ,and bar is the model inside the app.
I want to access the bar variable from the request object, not inside the ModelAdmin object.
So that won't be on the request object if you haven't put it there. You can get it from the url, but it looks llke you have answered that yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.