diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 709e19e..0f878ee 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -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:: diff --git a/relatives/model_admin.py b/relatives/model_admin.py index e500425..74630cd 100644 --- a/relatives/model_admin.py +++ b/relatives/model_admin.py @@ -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: diff --git a/relatives/tests/models.py b/relatives/tests/models.py index a1799b9..6a3a066 100644 --- a/relatives/tests/models.py +++ b/relatives/tests/models.py @@ -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): diff --git a/relatives/tests/tests.py b/relatives/tests/tests.py index 81fcc5a..0af1e23 100644 --- a/relatives/tests/tests.py +++ b/relatives/tests/tests.py @@ -254,3 +254,10 @@ def test_foreign_key_in_change_list(self): self.assertIn( b'Star of India', 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