Opened 10 months ago
Closed 41 hours ago
#36359 closed Bug (duplicate)
id_for_label undefined for AdminReadonlyField
| Reported by: | Marc Sommerhalder | Owned by: | |
|---|---|---|---|
| Component: | contrib.admin | Version: | 5.2 |
| Severity: | Normal | Keywords: | |
| Cc: | Lukas Klimas | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Change History (3)
comment:1 by , 10 months ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
comment:2 by , 9 days ago
| Cc: | added |
|---|---|
| Resolution: | invalid |
| Status: | closed → new |
AdminReadonlyField missing id_for_label definition creates a lot of log messages with big stacktraces of AttributeError: 'dict' object has no attribute 'id_for_label' or KeyError: 'id_for_label' .
In my case, project (Django 4.2) had structlog library for logging, which had rich traceback formatting enabled for the messages, thus creating big performance issues for admin pages. Example: with default implementation pages loaded in 2 seconds, with patched code it takes 600ms.
Patch applied from:
{% if field.field.help_text %}
<div class="help"{% if field.field.id_for_label %} id="{{ field.field.id_for_label }}_helptext"{% endif %}>
<div>{{ field.field.help_text|safe }}</div>
</div>
{% endif %}
to:
{% if field.field.help_text %}
<div class="help"{% if not field.is_readonly and field.field.id_for_label %} id="{{ field.field.id_for_label }}_helptext"{% endif %}>
<div>{{ field.field.help_text|safe }}</div>
</div>
{% endif %}
These log messages were logged under DEBUG level, thus changing the log level could also help.
To reproduce the problem requires:
- Model with the field, that has defined
help_textparameter. - Admin page for that model, that describes this field as
readonly
comment:3 by , 41 hours ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
Since this is just about silencing unhelpful, verbose logging from Django's own templates, we're tracking those as duplicates of #28526. I'm happy to iterate on your PR, so please just retitle it Refs #28526 -- ....
Hello Marc Sommerhalder, thanks for taking the time to report this and for referencing the relevant code.
You're correct that
AdminReadonlyFielddoes not defineid_for_label. However, from what I can see, it's only accessed conditionally (i.e. only if it's already defined) so there doesn’t appear to be any resulting error or broken behavior in the admin output:{% if field.field.id_for_label %} id="{{ field.field.id_for_label }}_helptext"{% endif %}To better understand the problem you're experiencing, could you clarify what concrete issue you're observing in practice? For example, is there a traceback, rendering problem, or integration issue that this is causing? That would help determine whether this needs a code change or if the current behavior is acceptable.
I'll close as
invalidfor now but please reopen if you can provide further details (ideally a test case failing or a small Django test project to reproduce the error or the issue you are getting). You can also reach out in the Django Forum for further assistance