Can't Allow Extra Fields in SQLModel using Pydantic's extra="allow"
option
#1210
-
First Check
Commit to Help
Example Code# Allowing extra fields works with BaseModel:
from pydantic import BaseModel
class Test(BaseModel, extra="allow"):
pass
Test()
# Output: Test()
test = Test(a="a")
print(test)
# Output: Test(a='a')
test.a
# Output: 'a'
# Allowing extra fields fails with SQLModel
from sqlmodel import SQLModel
class Test(SQLModel, extra="allow"):
pass
test = Test()
print(test)
# Output: Test()
Test(a="a")
# Output: Test()
test.a
# Output:
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "/workspaces/MADSci/.venv/lib/python3.9/site-packages/pydantic/main.py", line 856, in __getattr__
# raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
# AttributeError: 'Test' object has no attribute 'a' Description
Operating SystemLinux Operating System DetailsDev Container running in WSL 2 SQLModel Version0.0.22 Python VersionPython 3.9.2 Additional ContextFor my specific use case, I have a Model with many inheriting Models that I'm pre-validating when I read from a file, and have to query another source before I can do final validation with the appropriate submodel. I'd like to preserve the sub-model specific fields in the interim |
Beta Was this translation helpful? Give feedback.
Answered by
LuckierDodge
Nov 13, 2024
Replies: 1 comment
-
Credit to from sqlmodel import SQLModel
from pydantic import ConfigDict
class Test(SQLModel):
model_config = ConfigDict(extra="allow")
test = Test(a="a")
# Output: Test(a='a')
test.a
# Output: 'a' |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
LuckierDodge
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit to
brechin
in the discord for pointing out the correct implementation: