Skip to content

Commit

Permalink
SmartRelationshipsMixin : default exclude RelatedList
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoLechemia committed Nov 15, 2023
1 parent a0d0591 commit f4970f1
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/utils_flask_sqla/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from marshmallow.fields import Nested
from marshmallow_sqlalchemy.fields import RelatedList

# from flask_marshmallow.fields import RelatedList


class SmartRelationshipsMixin:
Expand All @@ -19,7 +22,7 @@ def __init__(self, *args, **kwargs):
# excluded fields at meta level are not even generated by auto-schema
if field is None:
continue
if isinstance(field, Nested):
if isinstance(field, Nested) or isinstance(field, RelatedList):
nested_fields.add(name)
elif field.metadata.get("exclude", False):
excluded_fields.add(name)
Expand All @@ -40,6 +43,7 @@ def __init__(self, *args, **kwargs):
exclude = kwargs.pop("exclude", None)
exclude = set(exclude) if exclude is not None else set()
exclude |= (excluded_fields | nested_fields) - firstlevel_only

# If only contains only nested & additional fields, we need to add included_fields to serialize nested, additional & included fields.
# If only does not contains nested or additional fields, we do nothing and marshmallow will serialize only specified fields.
if only and not firstlevel_only - nested_fields - additional_fields:
Expand Down

0 comments on commit f4970f1

Please sign in to comment.