From ed318dcd68bf335a3f5bf2b6de9aea868940aa86 Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Fri, 29 Nov 2024 10:24:48 +0100 Subject: [PATCH] feat(utils): update create_object_from_uri to work with generic uri The Uri model now works with a generic foreign key in addition to the a direct foreign key to the RootObject. This commit updates all occurences of `root_object` to also work with the generic foreign key. --- apis_core/utils/helpers.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/apis_core/utils/helpers.py b/apis_core/utils/helpers.py index a65f83e48..ef67cca5c 100644 --- a/apis_core/utils/helpers.py +++ b/apis_core/utils/helpers.py @@ -75,12 +75,18 @@ def create_object_from_uri(uri: str, model: object, raise_on_fail=False) -> obje if uri.startswith("http"): try: uri = Uri.objects.get(uri=uri) - return uri.root_object + return uri.content_object or uri.root_object except Uri.DoesNotExist: Importer = get_importer_for_model(model) importer = Importer(uri, model) instance = importer.create_instance() - uri = Uri.objects.create(uri=importer.get_uri, root_object=instance) + content_type = ContentType.objects.get_for_model(instance) + uri = Uri.objects.create( + uri=importer.get_uri, + root_object=instance, + content_type=content_type, + object_id=instance.id, + ) return instance if raise_on_fail: content_type = ContentType.objects.get_for_model(model)