#1038 closed defect (fixed)
[patch] ForeignKey uses wrong verbose_name by default
Reported by: | Esaj | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | Metasystem | Version: | |
Severity: | normal | Keywords: | |
Cc: | gary.wilson@… | Triage Stage: | Unreviewed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
ForeignKey
uses the verbose_name
of the target model by default. This is confusing when you have multiple ForeignKey
s pointing to the same model because in the admin screen you see the same label repeated for the ForeignKey
fields.
Index: django/core/meta/fields.py =================================================================== --- django/core/meta/fields.py (revision 1593) +++ django/core/meta/fields.py (working copy) @@ -690,10 +690,9 @@ to_name = to._meta.object_name.lower() except AttributeError: # to._meta doesn't exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT assert to == 'self', "ForeignKey(%r) is invalid. First parameter to ForeignKey must be either a model or the string %r" % (to, RECURSIVE_RELATIONSHIP_CONSTANT)- kwargs['verbose_name'] = kwargs.get('verbose_name', '') else: to_field = to_field or to._meta.pk.name - kwargs['verbose_name'] = kwargs.get('verbose_name', to._meta.verbose_name) + kwargs['verbose_name'] = kwargs.get('verbose_name', '') if kwargs.has_key('edit_inline_type'): import warnings
Attachments (1)
Change History (8)
by , 19 years ago
Attachment: | fields.py.diff added |
---|
comment:1 by , 19 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 19 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
Why is it castrating the behaviour at all? Even with a single foreign key, it seems more usable to use a verbose name derived from the actual field name, rather than the related model's name. For example:
class Bug: reported_by = meta.ForeignKey(auth.User)
The verbose name will be "user" here instead of "reported by". Surely "reported by" is more informative e.g. when displayed in the admin.
comment:4 by , 19 years ago
"The verbose name will be "user" here instead of "reported by". Surely "reported by" is more informative e.g. when displayed in the admin. "
agreed
comment:5 by , 19 years ago
"The verbose name will be "user" here instead of "reported by". Surely "reported by" is more informative e.g. when displayed in the admin."
Yes!
This is useful with one or more ForeignKeys. It is done with all the other fields, and I agree that it should be done here. And it means less typing too.
comment:6 by , 19 years ago
Cc: | added |
---|
comment:7 by , 19 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
If you have multiple ForeignKeys pointing to the same model, set verbose_name manually. We shouldn't castrate the behavior in 80% of cases (with only one ForeignKey to the same model) in favor of an edge case.