我正在使用dataclass
and asdict
from dataclasses
,当我引入继承时,我发现它asdict
不像我所期望的那样工作。
我dataclasses
用来帮助我从类创建字典,以便我可以将它们传递到django.shortcuts.render
.
from dataclasses import dataclass
from dataclasses import asdict
@dataclass
class Base:
name: str
class Test(Base):
def __init__(self, age, *args, **kwargs):
self.age = age
super(Test, self).__init__(*args, **kwargs)
test = Test(age=20, name="john doe")
print(asdict(test))
我希望输出是
{"age": 20, "name": "john doe"}
但我得到的只是基类的关键字值
{"name": "john doe"}