diff --git a/django_netjsonconfig/base/config.py b/django_netjsonconfig/base/config.py index 45827a2..6ae8810 100644 --- a/django_netjsonconfig/base/config.py +++ b/django_netjsonconfig/base/config.py @@ -231,3 +231,15 @@ def get_context(self): class Meta: abstract = True + + +def sortedm2m__str__(self): + """ + Improves string representation of m2m relationship objects + + TODO + ---- + this method can be removed if the following pull request + gets merged: https://github.com/gregmuellegger/django-sortedm2m/pull/101 + """ + return _('Relationship with {0}').format(self.template.name) diff --git a/django_netjsonconfig/models.py b/django_netjsonconfig/models.py index 54c47fc..b038134 100644 --- a/django_netjsonconfig/models.py +++ b/django_netjsonconfig/models.py @@ -1,4 +1,4 @@ -from .base.config import AbstractConfig, TemplatesVpnMixin +from .base.config import AbstractConfig, TemplatesVpnMixin, sortedm2m__str__ from .base.template import AbstractTemplate from .base.vpn import AbstractVpn, AbstractVpnClient @@ -11,13 +11,6 @@ class Meta(AbstractConfig.Meta): abstract = False -def sortedm2m__str__(self): - """ - Improves string representation of m2m relationship objects - """ - return self.template.name - - Config.templates.through.__str__ = sortedm2m__str__ diff --git a/django_netjsonconfig/tests/test_config.py b/django_netjsonconfig/tests/test_config.py index 00653ec..e6bdcc2 100644 --- a/django_netjsonconfig/tests/test_config.py +++ b/django_netjsonconfig/tests/test_config.py @@ -418,3 +418,10 @@ def test_vpn_context_no_cert(self): self.assertNotIn(key_contents_key, context) self.assertIn(ca_path_key, context) self.assertIn(ca_contents_key, context) + + def test_m2m_str_conversion(self): + t = self._create_template() + c = self._create_config(name='test-m2m-str-repr') + c.templates.add(t) + c.save() + self.assertIn('Relationship with', str(c.templates.through.objects.first()))