代码如下:
from dataclasses import dataclass
from dataclasses_json import dataclass_json
@dataclass_json
@dataclass
class Foo:
f: str
@dataclass_json
@dataclass
class Baz(Foo):
b: str
def full(self):
return self.to_dict()
# as expected, returns {"f":"f", "b":"b"}
def partial(self):
return Foo.to_dict(self)
# also returns {"f":"f", "b":"b"}
# how can I make it just return {"f":"f"}?
print(Baz(f="f", b="b").partial())
输出:
{"f":"f"}
如何限制partial
只返回f
而不同时返回b
和f
?
您可以使用Foo
类模式只输出Foo
上存在的字段
def partial(self):
return Foo.schema().dump(self)