diff --git a/src/marshmallow_sqlalchemy/convert.py b/src/marshmallow_sqlalchemy/convert.py index e5f26c0..99287a7 100644 --- a/src/marshmallow_sqlalchemy/convert.py +++ b/src/marshmallow_sqlalchemy/convert.py @@ -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 @@ -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 @@ -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") @@ -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( @@ -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 diff --git a/tests/test_sqlalchemy_schema.py b/tests/test_sqlalchemy_schema.py index 386e89c..bd2da0c 100644 --- a/tests/test_sqlalchemy_schema.py +++ b/tests/test_sqlalchemy_schema.py @@ -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