Skip to content

Commit

Permalink
Use verbose_name for related fields
Browse files Browse the repository at this point in the history
  • Loading branch information
treyhunner committed Dec 19, 2023
1 parent dc6ead4 commit f6948ff
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ To make migrations you can run::
Testing
-------

To install the package and its dependencies::

pip install -e .

Please add tests for your code and ensure existing tests don't break. To run
the tests against your code::

Expand Down
24 changes: 15 additions & 9 deletions relatives/model_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@
from .utils import object_link


def relation_link(related_field):
def relation_link(related_field_name):
def object_relation_link(original_obj):
obj = getattr(original_obj, related_field)
obj = getattr(original_obj, related_field_name)
if obj is not None:
return object_link(obj)

object_relation_link.__name__ = related_field
object_relation_link.__name__ = related_field_name
object_relation_link.allow_tags = True
object_relation_link.admin_order_field = related_field
object_relation_link.admin_order_field = related_field_name
return object_relation_link


def relation_link_from_field(related_field):
object_relation_link = relation_link(related_field.name)
object_relation_link.__name__ = related_field.verbose_name
return object_relation_link


def related_link_or_attribute(model, attr):
try:
relation = model._meta.get_field(attr).is_relation
field = model._meta.get_field(attr)
except FieldDoesNotExist:
relation = False
if relation:
return relation_link(attr)
pass
else:
return attr
if field.is_relation:
return relation_link_from_field(field)
return attr


class RelativesMixin:
Expand Down
2 changes: 1 addition & 1 deletion relatives/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Sailor(models.Model):
"""Sailors have an admin URL and sometimes link to ships"""

name = models.CharField(max_length=80)
ship = models.ForeignKey(Ship, null=True, on_delete=models.SET_NULL)
ship = models.ForeignKey(Ship, null=True, on_delete=models.SET_NULL, verbose_name="sea ship")

@property
def ship_name(self):
Expand Down
7 changes: 7 additions & 0 deletions relatives/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,10 @@ def test_foreign_key_in_change_list(self):
self.assertIn(
b'<a href="/adm/tests/ship/1/change/">Star of India</a>', response.content
)

def test_verbose_name_used_in_change_list(self):
self.login()
ship = Ship.objects.create(id=1, name="Star of India")
Sailor.objects.create(name="John Ford", ship=ship)
response = self.client.get(reverse("admin:tests_sailor_changelist"))
self.assertIn(b'sea ship', response.content) # verbose_name used

0 comments on commit f6948ff

Please sign in to comment.