Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/marshmallow_sqlalchemy/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,14 @@ def fields_for_model(
continue
if not include_relationships and hasattr(prop, "direction"):
continue
field = base_fields.get(key) or self.property2field(prop)
dump_only = bool(
self.schema_cls
and hasattr(self.schema_cls, "opts")
and key in getattr(self.schema_cls.opts, "dump_only", ())
)
field = base_fields.get(key) or self.property2field(
prop, **({"dump_only": True} if dump_only else {})
)
if field:
result[key] = field
return result
Expand All @@ -207,7 +214,14 @@ def fields_for_table(
continue
# Overridden fields are specified relative to key generated by
# self._get_key_for_column(...), rather than keys in source model
field = base_fields.get(key) or self.column2field(column)
dump_only = bool(
self.schema_cls
and hasattr(self.schema_cls, "opts")
and key in getattr(self.schema_cls.opts, "dump_only", ())
)
field = base_fields.get(key) or self.column2field(
column, **({"dump_only": True} if dump_only else {})
)
if field:
result[key] = field
return result
Expand Down Expand Up @@ -259,6 +273,8 @@ def property2field(
return field_class
field_kwargs = self._get_field_kwargs_for_property(prop)
field_kwargs.update(kwargs)
if field_kwargs.get("dump_only") and "required" not in kwargs:
field_kwargs["required"] = False
ret = field_class(**field_kwargs)
if (
hasattr(prop, "direction")
Expand Down Expand Up @@ -293,7 +309,10 @@ def column2field(
return field_class
field_kwargs = self.get_base_kwargs()
self._add_column_kwargs(field_kwargs, column)
return field_class(**{**field_kwargs, **kwargs})
field_kwargs.update(kwargs)
if field_kwargs.get("dump_only") and "required" not in kwargs:
field_kwargs["required"] = False
return field_class(**field_kwargs)

@overload
def field_for(
Expand Down Expand Up @@ -424,11 +443,16 @@ def _add_column_kwargs(self, kwargs: dict[str, Any], column: sa.Column) -> None:
if hasattr(column, "nullable"):
if column.nullable:
kwargs["allow_none"] = True
kwargs["required"] = not column.nullable and not _has_default(column)
kwargs["required"] = (
not column.nullable
and not _has_default(column)
and not kwargs.get("dump_only")
)
# If there is no nullable attribute, we are dealing with a property
# that does not derive from the Column class. Mark as dump_only.
else:
kwargs["dump_only"] = True
kwargs["required"] = False

if hasattr(column.type, "enum_class") and column.type.enum_class is not None:
kwargs["enum"] = column.type.enum_class
Expand Down
23 changes: 23 additions & 0 deletions tests/test_sqlalchemy_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,3 +750,26 @@ class Meta:
assert schema.dump(teacher) == {
"data": {"foo": "bar"},
}


def test_dump_only_field_not_required(models):
class StudentSchema(SQLAlchemySchema):
class Meta:
model = models.Student

full_name = auto_field(dump_only=True)

schema = StudentSchema()
assert schema.fields["full_name"].dump_only is True
assert schema.fields["full_name"].required is False


def test_auto_schema_meta_dump_only_not_required(models):
class StudentAutoSchema(SQLAlchemyAutoSchema):
class Meta:
model = models.Student
dump_only = ("full_name",)

schema = StudentAutoSchema()
assert schema.fields["full_name"].dump_only is True
assert schema.fields["full_name"].required is False