[Django]-Django DRY verbose names with foreign keys?

6👍

✅

Looking into the source code it seems like this is implemented in the set_attributes_from_rel method of the RelatedField class:

if self.verbose_name is None:
    self.verbose_name = self.remote_field.model._meta.verbose_name

The verbose_name is also set in the set_attributes_from_name method from the Field class:

if self.verbose_name is None and self.name:
    self.verbose_name = self.name.replace('_', ' ')

This method is run first and therefore when the method of the RelatedField class is run self.verbose_name is no longer None. It is unclear to me why the method of the Field class is run first.

Note: I checked the current source code on GitHub which is most likely a newer version than your code as your ForeignKey doesn’t have the on_delete parameter which was made required in version 2.0.

Leave a comment