Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#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 ForeignKeys 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)

fields.py.diff (914 bytes ) - added by Esaj 19 years ago.

Download all attachments as: .zip

Change History (8)

by Esaj, 19 years ago

Attachment: fields.py.diff added

comment:1 by Adrian Holovaty, 19 years ago

Resolution: invalid
Status: newclosed

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.

comment:2 by Esaj, 19 years ago

Resolution: invalid
Status: closedreopened

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:3 by James Bennett, 19 years ago

In that case, wouldn't related_name do what you want?

comment:4 by mordaha, 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 gary.wilson@…, 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 anonymous, 19 years ago

Cc: gary.wilson@… added

comment:7 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: reopenedclosed

(In [2342]) Fixed #1038 -- Changed ForeignKey to calculate verbose_name by looking at its field name rather than the model of the related object. Thanks, Esaj

Note: See TracTickets for help on using tickets.
Back to Top