@@ -66,50 +66,46 @@ class ProjectNodeCreate(BaseModel):
6666 parent : str | None = None
6767 boot_options : dict [str , Any ] | None = None
6868
69+ model_config = ConfigDict (frozen = True )
70+
6971 @classmethod
7072 def get_field_names (cls , * , exclude : set [str ]) -> set [str ]:
7173 return cls .model_fields .keys () - exclude
7274
75+ def _get_node_exclude_fields (self ) -> set [str ]:
76+ """Get the base fields to exclude when converting to Node model."""
77+ return {"node_id" , "required_resources" }
78+
7379 def model_dump_as_node (self ) -> dict [str , Any ]:
7480 """Converts a ProjectNode from the database to a Node model for the API.
7581
82+ Usage:
83+ Node.model_validate(project_node_create.model_dump_as_node(), by_name=True)
84+
7685 Handles field mapping and excludes database-specific fields that are not
7786 part of the Node model.
7887 """
79- # Get all ProjectNode fields except those that don't belong in Node
80- exclude_fields = {"node_id" , "required_resources" }
8188 return self .model_dump (
8289 # NOTE: this setup ensures using the defaults provided in Node model when the db does not
8390 # provide them, e.g. `state`
84- exclude = exclude_fields ,
91+ exclude = self . _get_node_exclude_fields () ,
8592 exclude_none = True ,
8693 exclude_unset = True ,
8794 )
8895
89- model_config = ConfigDict (frozen = True )
90-
9196
9297class ProjectNode (ProjectNodeCreate ):
9398 created : datetime .datetime
9499 modified : datetime .datetime
95100
96101 model_config = ConfigDict (from_attributes = True )
97102
98- def model_dump_as_node (self ) -> dict [str , Any ]:
99- """Converts a ProjectNode from the database to a Node model for the API.
103+ def _get_node_exclude_fields (self ) -> set [str ]:
104+ """Get the fields to exclude when converting to Node model, including DB-specific fields."""
105+ base_excludes = super ()._get_node_exclude_fields ()
106+ return base_excludes | {"created" , "modified" }
100107
101- Handles field mapping and excludes database-specific fields that are not
102- part of the Node model.
103- """
104- # Get all ProjectNode fields except those that don't belong in Node
105- exclude_fields = {"node_id" , "required_resources" , "created" , "modified" }
106- return self .model_dump (
107- # NOTE: this setup ensures using the defaults provided in Node model when the db does not
108- # provide them, e.g. `state`
109- exclude = exclude_fields ,
110- exclude_none = True ,
111- exclude_unset = True ,
112- )
108+ # NOTE: model_dump_as_node is inherited from ProjectNodeCreate and uses the overridden _get_node_exclude_fields
113109
114110
115111@dataclass (frozen = True , kw_only = True )
0 commit comments