@@ -712,6 +712,9 @@ class LoadEntityParam(BaseModel):
712712 autofetch_schema : Optional [bool ] = True
713713 """If true, load the corresponding schemas /
714714 categories ad-hoc if not already present"""
715+ model_to_use : Optional [Type [OswBaseModel ]] = None
716+ """If provided this model will be used to create an entity (instance of the
717+ model), instead of instantiating the autofetched schema."""
715718 remove_empty : Optional [bool ] = True
716719 """If true, remove key with an empty string, list, dict or set as value
717720 from the jsondata."""
@@ -770,6 +773,9 @@ def load_entity(
770773 else :
771774 param = entity_title
772775
776+ if param .model_to_use :
777+ print (f"Using schema { param .model_to_use .__name__ } to create entity" )
778+
773779 # store original cache state
774780 cache_state = self .site .get_cache_enabled ()
775781 if param .disable_cache :
@@ -797,23 +803,29 @@ def load_entity(
797803 schemas .append (schema )
798804 # generate model if not already exists
799805 cls_name : str = schema ["title" ]
800- if not hasattr (model , cls_name ):
801- if param .autofetch_schema :
802- self .fetch_schema (
803- OSW .FetchSchemaParam (
804- schema_title = category , mode = "append"
806+ # If a schema_to_use is provided, we do not need to check if the
807+ # model exists
808+ if not param .model_to_use :
809+ if not hasattr (model , cls_name ):
810+ if param .autofetch_schema :
811+ self .fetch_schema (
812+ OSW .FetchSchemaParam (
813+ schema_title = category , mode = "append"
814+ )
805815 )
816+ if not hasattr (model , cls_name ):
817+ schemas_fetched = False
818+ print (
819+ f"Error: Model { cls_name } not found. Schema { category } "
820+ f"needs to be fetched first."
806821 )
807- if not hasattr (model , cls_name ):
808- schemas_fetched = False
809- print (
810- f"Error: Model { cls_name } not found. Schema { category } "
811- f"needs to be fetched first."
812- )
813822 if not schemas_fetched :
814823 continue
815824
816- if len (schemas ) == 0 :
825+ if param .model_to_use :
826+ entity : model .OswBaseModel = param .model_to_use (** jsondata )
827+
828+ elif len (schemas ) == 0 :
817829 print ("Error: no schema defined" )
818830
819831 elif len (schemas ) == 1 :
0 commit comments