From 2f53411efcabec5651684715fa820e13a4a5e78b Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 8 Jun 2020 16:32:50 -0400 Subject: [PATCH] Extend assertInstanceEqual() to handle M2M relations to ContentType --- netbox/utilities/testing/views.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/netbox/utilities/testing/views.py b/netbox/utilities/testing/views.py index 1ba96e395..b3c35ed87 100644 --- a/netbox/utilities/testing/views.py +++ b/netbox/utilities/testing/views.py @@ -1,6 +1,7 @@ from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ObjectDoesNotExist +from django.db.models import ForeignKey, ManyToManyField from django.forms.models import model_to_dict from django.test import Client, TestCase as _TestCase, override_settings from django.urls import reverse, NoReverseMatch @@ -83,12 +84,15 @@ class TestCase(_TestCase): if api: # Replace ContentType numeric IDs with . - if type(getattr(instance, key)) is ContentType: + field = instance._meta.get_field(key) + if type(field) is ForeignKey and field.related_model is ContentType: ct = ContentType.objects.get(pk=value) model_dict[key] = f'{ct.app_label}.{ct.model}' + elif type(field) is ManyToManyField and field.related_model is ContentType: + model_dict[key] = [f'{ct.app_label}.{ct.model}' for ct in value] # Convert IPNetwork instances to strings - if type(value) is IPNetwork: + elif type(value) is IPNetwork: model_dict[key] = str(value) # Omit any dictionary keys which are not instance attributes