Opened 11 years ago
Closed 11 years ago
#23167 closed New feature (fixed)
BaseForm lacks a useful repr
| Reported by: | Owned by: | nobody | |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Areski Belaid | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Currently, form instances have a useful __str__ which renders them out as a table, but the __repr__ (or lack thereof) for them is the same old unhelpful <module.FormName at 0xDEADBEEF> which all Python classes get by default.
It would be more useful to those of us who live in pdb/repl (and debug-toolbar template contexts -- which use pprint.pformat for rendering values, which internally uses saferepr, which as you might expect, eventually calls repr(x)) if the repr had some info about the form itself.
Off the top of my head, is_bound, field names, valid state would be a reasonable representation of the form, such that you get something like <module.FormName is_bound=True, is_valid=False, fields=('a', 'b', 'c')>
A slight complication is that the is_valid bool couldn't be obtained via is_valid because that triggers validation -- it would instead need to be is_bound and not self._errors or something (I'm assuming ErrorDict returns truthy/falsy the same as dict does)
Further, the field names ought to come from the keys of self.fields rather than base_fields, as the former is instance specific and often gets modified.
One could also make the case for adding has_changed into the repr, but in my experience, it's a lesser-used piece of the API; ditto the prefix.
There've been a few tickets for repr-adding goodness (#22906, #22531, #19543) so I'm assuming that providing a richer REPL experience in general means there may be merit to this ticket.
Change History (3)
comment:1 by , 11 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 11 years ago
| Cc: | added |
|---|---|
| Has patch: | set |
comment:3 by , 11 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
In a6691e5dcfdfd1529987be3bdcf06e7ab9948356:
Fixed #23167 -- Added BaseForm.repr()
Thanks Keryn Knight for the idea.
Here a PR: https://github.com/django/django/pull/3097
Keryn, do you see this satisfactory?