# How to get the value of rendered field in django admin

**URL:** <https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399>\
**Category:** The Admin\
**Created:** [March 7, 2026, 8:17am UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399 "2026-03-07T08:17:53Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mia/32/24939_2.png) [@mia](https://forum.djangoproject.com/u/mia)\
**Post date:** [March 7, 2026, 8:17am UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/1 "2026-03-07T08:17:53Z")

</div>

I create pytest test and want to get rendered field which logic can change depending from user. It can be tricky due to a singleton problem in the admin [How to solve the singleton problem in Django ModelAdmin. - DEV Community](https://dev.to/danilovmy/how-to-solve-the-singleton-problem-in-django-modeladmin-g42). The code is like:

```auto
from django.test import Client
from models import Message

@pytest.fixture(scope="session")
def manager1_browser_client(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        manager = User.objects.get(username='manager1')
        client = Client()
        client.manager_user = manager
        client.login(username=manager.username, password=settings.ADMIN_PASSWORD)
        yield client

def test_manager_cannot_see_sensitive_data(manager1_browser_client):
    ...
    msg = Message.objects.create()
    url = reverse(f"admin:{app_name}_{message_model_name}_change", args=[msg.pk])
    response = manager1_browser_client.get(url)

    # How to assert, that restricted data is not in field of msg.data in the admin pannel, but can be in another rendered fields of this object? 

```

I get from manager1\_browser\_client.get(url) the response object. And this not works response.context.keys() with error

{TypeError}TypeError(“unhashable type: ‘ContextDict’”)

Why does it happen? I would like to get all possible keys, because I can’t get my field value by response.context[‘data’] that was discussed here [How to access context from a custom django-admin command?](https://forum.djangoproject.com/t/how-to-access-context-from-a-custom-django-admin-command/18356) .

I want to get the value that was returned in the reality in one of fileds for test. And I don’t want to call ModelAdmin object directly or use beautifullsoup for that. Can I do it via .context? Or are any other good ways?

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [March 11, 2026, 11:38pm UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/2 "2026-03-11T23:38:07Z")

</div>

Did you print the response object? What you get? `print(type(response.context))`

Print here also this:  
`context_dict = dict(response.context) print(context_dict.keys())`

---

<div class="post-metadata">

**Author:** ![mia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mia/32/24939_2.png) [@mia](https://forum.djangoproject.com/u/mia)\
**Post date:** [March 13, 2026, 7:38am UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/3 "2026-03-13T07:38:07Z")

</div>

> [@anefta](#):
>
> type(response.context

\<class ‘django.test.utils.ContextList’\>

> [@anefta](#):
>
> context\_dict = dict(response.context)

{TypeError}TypeError(“unhashable type: ‘ContextDict’”)

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [March 13, 2026, 9:40am UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/4 "2026-03-13T09:40:06Z")

</div>

Ok, let’s try this :  
`flat_context = response.context.flatten()`  
`print(flat_context.keys())`

I suspect the flattening is messed up somehow when you directly call `response.context.keys()`

---

<div class="post-metadata">

**Author:** ![mia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mia/32/24939_2.png) [@mia](https://forum.djangoproject.com/u/mia)\
**Post date:** [March 13, 2026, 1:24pm UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/5 "2026-03-13T13:24:46Z")

</div>

> [@anefta](#):
>
> flat\_context = response.context.flatten()

{AttributeError}AttributeError(“‘ContextList’ object has no attribute ‘flatten’”)

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [March 16, 2026, 2:00pm UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/6 "2026-03-16T14:00:54Z")

</div>

```auto
safe_keys = set()

for ctx in response.context:
    
    for d in ctx.dicts: 
        for key in d.keys():
            if isinstance(key, str): 
                safe_keys.add(key)

print(sorted(safe_keys))

```

---

<div class="post-metadata">

**Author:** ![mia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mia/32/24939_2.png) [@mia](https://forum.djangoproject.com/u/mia)\
**Post date:** [March 16, 2026, 3:11pm UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/7 "2026-03-16T15:11:06Z")

</div>

> ```
> for key in d.keys():
> 
> ```

E AttributeError: ‘RequestContext’ object has no attribute ‘keys’

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [March 16, 2026, 4:23pm UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/8 "2026-03-16T16:23:19Z")

</div>

```auto
safe_keys = set()

for ctx in response.context:

   flat_dict = ctx.flatten()
   safe_keys.update(flat_dict.keys())

print(sorted(safe_keys))

```

---

<div class="post-metadata">

**Author:** ![mia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mia/32/24939_2.png) [@mia](https://forum.djangoproject.com/u/mia)\
**Post date:** [March 17, 2026, 8:11pm UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/9 "2026-03-17T20:11:07Z")

</div>

```auto
        for ctx in response.context:
> flat_dict = ctx.flatten()

... 

    def flatten(self):
        """
        Return self.dicts as one dictionary.
        """
        flat = {}
        for d in self.dicts:
> flat.update(d)
E ValueError: dictionary update sequence element #0 has length 1; 2 is required

d = [{'True': True, 'False': False, 'None': None}, {}, {}, {'site_title': 'Django Admin', 'site_header': '...rib.admin.templatetags.base.InclusionAdminNode object at 0x730ee7e76fe0>, <TextNode: '\n\n</div>\n</form></div>\n'>]>}]
flat = {'False': False, 'None': None, 'True': True}
self = [{'True': True, 'False': False, 'None': None}, [{'True': True, 'False': False, 'None': None}, {}, {}, {'site_title': '...ib.admin.templatetags.base.InclusionAdminNode object at 0x730ee7e76fe0>, <TextNode: '\n\n</div>\n</form></div>\n'>]>}]]

/usr/local/lib/python3.10/site-packages/django/template/context.py:120: ValueError

```

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [March 19, 2026, 11:54pm UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/10 "2026-03-19T23:54:25Z")

</div>

```auto
safe_keys = set()
for ctx in response.context:
    for d in getattr(ctx, 'dicts', []):
        if isinstance(d, dict):
            safe_keys.update([k for k in d.keys() if isinstance(k, str)])
       
        elif isinstance(d, list):
            for item in d:
                if isinstance(item, dict):
                    safe_keys.update([k for k in item.keys() if isinstance(k, str)])

print(sorted(safe_keys))

```

---

<div class="post-metadata">

**Author:** ![mia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mia/32/24939_2.png) [@mia](https://forum.djangoproject.com/u/mia)\
**Post date:** [March 20, 2026, 12:59pm UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/11 "2026-03-20T12:59:50Z")

</div>

The output is:

```auto
[
  "DEFAULT_MESSAGE_LEVELS",
  "False",
  "LANGUAGE_BIDI",
  "LANGUAGE_CODE",
  "None",
  "True",
  "absolute_url",
  "add",
  "add_related_url",
  "adminform",
  "app_label",
  "app_list",
  "attrs",
  "available_apps",
  "block",
  "can_add_related",
  "can_change_related",
  "can_delete_related",
  "can_view_related",
  "change",
  "change_related_template_url",
  "content_type_id",
  "csrf_token",
  "date_label",
  "docsroot",
  "error_class",
  "errors",
  "field",
  "fieldset",
  "forloop",
  "form_url",
  "group_choices",
  "group_index",
  "group_name",
  "has_absolute_url",
  "has_add_permission",
  "has_change_permission",
  "has_delete_permission",
  "has_editable_inline_admin_formsets",
  "has_file_field",
  "has_permission",
  "has_view_permission",
  "inline_admin_formsets",
  "is_hidden",
  "is_nav_sidebar_enabled",
  "is_popup",
  "is_popup_var",
  "label",
  "log_entries",
  "media",
  "messages",
  "model",
  "model_has_limit_choices_to",
  "name",
  "object_id",
  "option",
  "opts",
  "original",
  "perms",
  "preserved_filters",
  "rendered_widget",
  "request",
  "save_as",
  "save_on_top",
  "show_changelinks",
  "site_header",
  "site_title",
  "site_url",
  "subtitle",
  "tag",
  "time_label",
  "title",
  "to_field",
  "to_field_var",
  "url_params",
  "use_tag",
  "user",
  "view_related_url_params",
  "widget"
]

```

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [March 23, 2026, 5:18pm UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/12 "2026-03-23T17:18:49Z")

</div>

That is the exact list of variables the Django Admin passes to the template to render the page. Does it make sense?

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [March 23, 2026, 5:22pm UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/13 "2026-03-23T17:22:08Z")

</div>

> [@mia](#):
>
> `"adminform",`

Do you see it?  
So something like that might work:

```auto
def get_rendered_field_value(adminform, field_name):

for fieldset in adminform:
    for line in fieldset:
        for admin_field in line:
            # Read-only fields store the name in a dict; editable fields store it as an attribute
            current_name = admin_field.field['name'] if admin_field.is_readonly else admin_field.field.name
            
            if current_name == field_name:
                if admin_field.is_readonly:
                    return admin_field.contents() # The HTML/text rendered to the user
                return admin_field.field.value() # The value passed to the form widget
                
return None # Field is not rendered on the page at all

```

---

<div class="post-metadata">

**Author:** ![mia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mia/32/24939_2.png) [@mia](https://forum.djangoproject.com/u/mia)\
**Post date:** [March 23, 2026, 11:03pm UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/14 "2026-03-23T23:03:47Z")

</div>

Nice! It works `get_rendered_field_value(response.context['adminform'], "my_field")`

Thank you so much for your guidance!

So, is the iteration by adminform and fieldsets the one way to do it? I can’t just index them or get values by key as I see.

---

<div class="post-metadata">

**Author:** ![anefta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anefta/32/10649_2.png) [@anefta](https://forum.djangoproject.com/u/anefta)\
**Post date:** [March 24, 2026, 10:05am UTC](https://forum.djangoproject.com/t/how-to-get-the-value-of-rendered-field-in-django-admin/44399/15 "2026-03-24T10:05:54Z")

</div>

It seems that in order to get the _rendered_ value of any field (especially read-only ones), iteration is the best way to do it. You cannot just index `adminform['my_field']` directly because `adminform` is not a dictionary; it is a specialized generator object designed specifically to yield HTML layout blocks (`Fieldsets`, which yield `Lines`, which yield `Fields`).
