Skip to content

Commit 0586747

Browse files
Add exclude parameter to to_model
1 parent ff576d1 commit 0586747

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/aiida/orm/entities.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ def orm_to_model_field_values(
292292
*,
293293
repository_path: Optional[pathlib.Path] = None,
294294
serialize_repository_content: bool = False,
295+
exclude: Optional[set[str]] = None,
295296
skip_read_only: bool = False,
296297
) -> dict[str, Any]:
297298
"""Collect values for the ``Model``'s fields from this entity.
@@ -301,6 +302,7 @@ def orm_to_model_field_values(
301302
302303
:param repository_path: Optional path to use for repository-based fields.
303304
:param serialize_repository_content: Whether to include repository file content.
305+
:param exclude: Optional set of field names to exclude.
304306
:param skip_read_only: When True, fields marked with ``exclude_to_orm`` are skipped.
305307
:return: Mapping of field name to value.
306308
"""
@@ -309,6 +311,9 @@ def orm_to_model_field_values(
309311
Model = self.Model if self.is_stored else self.CreateModel # noqa: N806
310312

311313
for key, field in Model.model_fields.items():
314+
if key in (exclude or {}):
315+
continue
316+
312317
if skip_read_only and get_metadata(field, 'exclude_to_orm'):
313318
continue
314319

@@ -328,18 +333,21 @@ def to_model(
328333
*,
329334
repository_path: Optional[pathlib.Path] = None,
330335
serialize_repository_content: bool = False,
336+
exclude: Optional[set[str]] = None,
331337
) -> Model:
332338
"""Return the entity instance as an instance of its model.
333339
334340
:param repository_path: If the orm node has files in the repository, this path is used to read the repository
335341
files from. If no path is specified a temporary path is created using the entities pk.
336342
:param serialize_repository_content: If True, repository file content is serialized in the model.
337343
This field can be very large, so it is excluded by default.
344+
:param exclude: Optional set of fields to exclude from the model.
338345
:return: An instance of the entity's model class.
339346
"""
340347
fields = self.orm_to_model_field_values(
341348
repository_path=repository_path,
342349
serialize_repository_content=serialize_repository_content,
350+
exclude=exclude,
343351
)
344352
Model = self.Model if self.is_stored else self.CreateModel # noqa: N806
345353
return Model(**fields)
@@ -359,6 +367,7 @@ def serialize(
359367
*,
360368
repository_path: Optional[pathlib.Path] = None,
361369
serialize_repository_content: bool = False,
370+
exclude: Optional[set[str]] = None,
362371
mode: Literal['json', 'python'] = 'json',
363372
) -> dict[str, Any]:
364373
"""Serialize the entity instance to JSON.
@@ -367,6 +376,7 @@ def serialize(
367376
files to. If no path is specified a temporary path is created using the entities pk.
368377
:param serialize_repository_content: If True, repository file content is serialized in the model.
369378
This field can be very large, so it is excluded by default.
379+
:param exclude: Optional set of fields to exclude from the model.
370380
:param mode: The serialization mode, either 'json' or 'python'. The 'json' mode is the most strict and ensures
371381
that the output is JSON serializable, whereas the 'python' mode allows for more complex Python types, such
372382
as `datetime` objects.
@@ -390,6 +400,7 @@ def serialize(
390400
return self.to_model(
391401
repository_path=repository_path,
392402
serialize_repository_content=serialize_repository_content,
403+
exclude=exclude,
393404
).model_dump(mode=mode)
394405

395406
@classmethod

0 commit comments

Comments
 (0)