diff --git a/netbox/utilities/utils.py b/netbox/utilities/utils.py index c3ad7527c..d7232d41b 100644 --- a/netbox/utilities/utils.py +++ b/netbox/utilities/utils.py @@ -571,14 +571,14 @@ def local_now(): def get_related_models(model, ordered=True): """ - Return a list of all models which have a ForeignKey to the given model and the name of the field. + Return a list of all models which have a ForeignKey to the given model and the name of the field. For example, + `get_related_models(Tenant)` will return all models which have a ForeignKey relationship to Tenant. """ - related_models = [] - for field in model._meta.get_fields(): - if type(field) is ManyToOneRel: - related_models.append( - (field.related_model, field.remote_field.name) - ) + related_models = [ + (field.related_model, field.remote_field.name) + for field in model._meta.related_objects + if type(field) is ManyToOneRel + ] if ordered: return sorted(related_models, key=lambda x: x[0]._meta.verbose_name)