mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
Optimize path node representations
This commit is contained in:
parent
a072d40594
commit
2c9ae60dec
@ -5,12 +5,20 @@ from .exceptions import CableTraceSplit
|
|||||||
|
|
||||||
|
|
||||||
def object_to_path_node(obj):
|
def object_to_path_node(obj):
|
||||||
return f'{obj._meta.model_name}:{obj.pk}'
|
"""
|
||||||
|
Return a representation of an object suitable for inclusion in a CablePath path. Node representation is in the
|
||||||
|
form <ContentType ID>:<Object ID>.
|
||||||
|
"""
|
||||||
|
ct = ContentType.objects.get_for_model(obj)
|
||||||
|
return f'{ct.pk}:{obj.pk}'
|
||||||
|
|
||||||
|
|
||||||
def path_node_to_object(repr):
|
def path_node_to_object(repr):
|
||||||
model_name, object_id = repr.split(':')
|
"""
|
||||||
model_class = ContentType.objects.get(model=model_name).model_class()
|
Given a path node representation, return the corresponding object.
|
||||||
|
"""
|
||||||
|
ct_id, object_id = repr.split(':')
|
||||||
|
model_class = ContentType.objects.get(pk=ct_id).model_class()
|
||||||
return model_class.objects.get(pk=int(object_id))
|
return model_class.objects.get(pk=int(object_id))
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user