Skip to content

Commit d6e59b9

Browse files
committed
Only pass Model.__fields__ when casting event args (#4356)
Attempting to initialize relationship fields in a sqlmodel model throws an error, so only pass defined pydantic __fields__ if the type is a Model.
1 parent cc337a8 commit d6e59b9

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

reflex/state.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from reflex.config import get_config
4747
from reflex.istate.data import RouterData
4848
from reflex.istate.storage import ClientStorageBase
49+
from reflex.model import Model
4950
from reflex.vars.base import (
5051
ComputedVar,
5152
DynamicRouteVar,
@@ -1733,15 +1734,20 @@ async def _process_event(
17331734
if value is None:
17341735
continue
17351736
hinted_args = value_inside_optional(hinted_args)
1736-
if (
1737-
isinstance(value, dict)
1738-
and inspect.isclass(hinted_args)
1739-
and (
1740-
dataclasses.is_dataclass(hinted_args)
1741-
or issubclass(hinted_args, Base)
1742-
)
1743-
):
1744-
payload[arg] = hinted_args(**value)
1737+
if isinstance(value, dict) and inspect.isclass(hinted_args):
1738+
if issubclass(hinted_args, Model):
1739+
# Remove non-fields from the payload
1740+
payload[arg] = hinted_args(
1741+
**{
1742+
key: value
1743+
for key, value in value.items()
1744+
if key in hinted_args.__fields__
1745+
}
1746+
)
1747+
elif dataclasses.is_dataclass(hinted_args) or issubclass(
1748+
hinted_args, Base
1749+
):
1750+
payload[arg] = hinted_args(**value)
17451751
if isinstance(value, list) and (hinted_args is set or hinted_args is Set):
17461752
payload[arg] = set(value)
17471753
if isinstance(value, list) and (

0 commit comments

Comments
 (0)