Code

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#1038 closed defect (fixed)

[patch] ForeignKey uses wrong verbose_name by default

Reported by: Esaj Owned by: adrian
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: UI/UX:

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 8 years ago.

Download all attachments as: .zip

Change History (8)

Changed 8 years ago by Esaj

comment:1 Changed 8 years ago by adrian

  • Resolution set to invalid
  • Status changed from new to 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.

comment:2 Changed 8 years ago by Esaj

  • Resolution invalid deleted
  • Status changed from closed to 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:3 Changed 8 years ago by ubernostrum

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

comment:4 Changed 8 years ago by mordaha

"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 Changed 8 years ago by gary.wilson@…

"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 Changed 8 years ago by anonymous

  • Cc gary.wilson@… added

comment:7 Changed 8 years ago by adrian

  • Resolution set to fixed
  • Status changed from reopened to closed

(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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.