Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pydantic - Dynamically create a model with multiple base classes?

From the pydantic docs I understand this:

import pydantic

class User(pydantic.BaseModel):
    id: int
    name: str

class Student(pydantic.BaseModel):
    semester: int

# this works as expected
class Student_User(User, Student):
    building: str

print(Student_User.__fields__.keys())
#> dict_keys(['semester', 'id', 'name', 'building'])

However, when I want to create a similar object dynamically (following the section dynamic-model-creation):

# this results in a TypeError
pydantic.create_model("Student_User2", __base__=(User, Student))

I get:

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Question: How to dynamically create a class like Student_User

like image 351
cknoll Avatar asked Jun 14 '26 04:06

cknoll


1 Answers

Its not the answer to the original question, but if you are like me and all you care about is having a model which holds fields of other models, this should be a solutions.

Student_User = pydantic.create_model("Student_User", **{
    **{key: (value.type_, value.default) for key, value in User.__fields__.items()},
    **{key: (value.type_, value.default) for key, value in Student.__fields__.items()},
    **{"building": (str, '')},
})

Essentially, we are dynamically creating a new pydantic model and we are setting its fields to be the fields of our other models plus an additional custom field.

Note: OP included these lines in his question:

print(Student_User.__fields__.keys())
#> dict_keys(['semester', 'id', 'name', 'building'])

So, my guess is that his end goal was copying the fields from the other models and having a model created from multiple bases was just a method of achieving it.

like image 54
OriFl Avatar answered Jun 15 '26 18:06

OriFl