Skip to content

Commit b9f45f9

Browse files
authored
Make round trip exclude computed fields (#934)
1 parent 34fbd84 commit b9f45f9

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/serializers/computed_fields.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ impl ComputedFields {
4848
exclude: Option<&PyAny>,
4949
extra: &Extra,
5050
) -> PyResult<()> {
51+
if extra.round_trip {
52+
// Do not serialize computed fields
53+
return Ok(());
54+
}
5155
for computed_fields in &self.0 {
5256
computed_fields.to_python(model, output_dict, filter, include, exclude, extra)?;
5357
}
@@ -63,6 +67,10 @@ impl ComputedFields {
6367
exclude: Option<&PyAny>,
6468
extra: &Extra,
6569
) -> Result<(), S::Error> {
70+
if extra.round_trip {
71+
// Do not serialize computed fields
72+
return Ok(());
73+
}
6674
for computed_field in &self.0 {
6775
let property_name_py = computed_field.property_name_py.as_ref(model.py());
6876
if let Some((next_include, next_exclude)) = filter

tests/serializers/test_model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,9 @@ def area(self) -> bytes:
570570
assert s.to_python(Model(width=3, height=4), mode='json') == {'width': 3, 'height': 4, 'area': '12'}
571571
assert s.to_json(Model(width=3, height=4)) == b'{"width":3,"height":4,"area":"12"}'
572572

573+
assert s.to_python(Model(width=3, height=4), round_trip=True) == {'width': 3, 'height': 4}
574+
assert s.to_json(Model(width=3, height=4), round_trip=True) == b'{"width":3,"height":4}'
575+
573576

574577
def test_property_alias():
575578
@dataclasses.dataclass

0 commit comments

Comments
 (0)