是否可以扩展使用DataModel-Code-Menerator生成的Pydantic模型?
我正在使用 datamodel-code-generator 生成 pydantic Models 来自JSON模式。
这是 json schema> json Schema 使用。
以及运行DataModel代码生成器后生成的模型。
# File: datamodel.py
from __future__ import annotations
from typing import List
from pydantic import BaseModel
class Record(BaseModel):
id: int
name: str
class Table(BaseModel):
records: List[Record]
class Globals(BaseModel):
table: Table
我一直在尝试扩展具有新属性的生成类。
# File: extensions.py
import json
from datamodel import Table, Globals
class ExtendedTable(Table):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
print('ExtendedTable Constructor')
# Won't work because "ExtendedTable" object has no field "records_by_id"
self.records_by_id = {record.id: record for record in self.records}
class ExtendedGlobals(Globals):
def __init__(self, table: ExtendedTable):
super().__init__(table=table)
print('ExtendedGlobals Constructor')
if __name__ == '__main__':
records = '''
{
"table": {
"records": [{"id": 0, "name": "A"}, {"id": 1, "name": "B"}]
}
}
'''
content = json.loads(records)
# Both won't call ExtendedTable.__init__()
ExtendedGlobals(**content)
ExtendedGlobals.parse_obj(content)
ExtendedGlobals(table=ExtendedTable(**content['table']))
但是,我还没有找到使Globals类使用表的扩展定义的方法。 另外,仅在子类中添加新字段似乎不起作用。
有没有办法扩展这些类而不必修改Pydantic生成的模型?还是另一个从JSON模式生成Python代码的工具?
I am using the datamodel-code-generator to generate pydantic models from a JSON schema.
Here is the JSON schema used.
And the generated models after running the datamodel-code-generator.
# File: datamodel.py
from __future__ import annotations
from typing import List
from pydantic import BaseModel
class Record(BaseModel):
id: int
name: str
class Table(BaseModel):
records: List[Record]
class Globals(BaseModel):
table: Table
I've been trying to extend the generated classes with new attributes.
# File: extensions.py
import json
from datamodel import Table, Globals
class ExtendedTable(Table):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
print('ExtendedTable Constructor')
# Won't work because "ExtendedTable" object has no field "records_by_id"
self.records_by_id = {record.id: record for record in self.records}
class ExtendedGlobals(Globals):
def __init__(self, table: ExtendedTable):
super().__init__(table=table)
print('ExtendedGlobals Constructor')
if __name__ == '__main__':
records = '''
{
"table": {
"records": [{"id": 0, "name": "A"}, {"id": 1, "name": "B"}]
}
}
'''
content = json.loads(records)
# Both won't call ExtendedTable.__init__()
ExtendedGlobals(**content)
ExtendedGlobals.parse_obj(content)
ExtendedGlobals(table=ExtendedTable(**content['table']))
However, I haven't found a way to make the Globals class use the extended definition of the table.
Also, simply adding new fields to the subclass does not seem to work.
Is there a way to extend these classes without having to modify the pydantic generated models? Or maybe another tool to generate Python code from JSON schema?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用所需类型再次声明该字段,则可以更改子类中的字段类型。
它看起来您正在设置
__ init __ init __()
方法中的实例属性,但是将字段声明为类属性。此示例显示了一种将计算出的字段
Records_by_id
添加到ExtendedTable
并使用ExtendedTable
inextendedGlobals
:输出:输出:输出:输出:
You can change the type of a field in a subclass if you declare the field again using the desired type.
It looks like you are setting instance attributes in the
__init__()
method, but fields are declared as class attributes.This example shows a way to add a calculated field
records_by_id
toExtendedTable
and useExtendedTable
inExtendedGlobals
:Output: