1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Move utilities.utils.get_related_models() to utilities.relations

This commit is contained in:
Jeremy Stretch
2024-03-21 16:12:44 -04:00
parent b86c8a9524
commit eb3aa7cb36
2 changed files with 5 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
from django.db.models import ManyToOneRel
__all__ = (
'get_related_models',
)
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. For example,
`get_related_models(Tenant)` will return all models which have a ForeignKey relationship to Tenant.
"""
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.lower())
return related_models