mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-23 07:56:44 -06:00
Reference Meta.related_objects instead of calling get_fields()
This commit is contained in:
parent
c36386a32b
commit
a60b266285
@ -571,14 +571,14 @@ def local_now():
|
|||||||
|
|
||||||
def get_related_models(model, ordered=True):
|
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 = []
|
related_models = [
|
||||||
for field in model._meta.get_fields():
|
(field.related_model, field.remote_field.name)
|
||||||
if type(field) is ManyToOneRel:
|
for field in model._meta.related_objects
|
||||||
related_models.append(
|
if type(field) is ManyToOneRel
|
||||||
(field.related_model, field.remote_field.name)
|
]
|
||||||
)
|
|
||||||
|
|
||||||
if ordered:
|
if ordered:
|
||||||
return sorted(related_models, key=lambda x: x[0]._meta.verbose_name)
|
return sorted(related_models, key=lambda x: x[0]._meta.verbose_name)
|
||||||
|
Loading…
Reference in New Issue
Block a user