Hi there, I’m trying to understand the state flow of the customer_data_app example.
In the State class, current_user is defined as a Customer type (a table) with current_user: Customer = Customer()
However, everywhere in the backend self.current_user is treated as a dict, for example: select(Customer).where(Customer.email == self.current_user["email"])
Then in this method, print(type(user)) returns <class 'dict'> too:
This is a bit of wrinkle that probably should be resolved in the framework.
For event handlers, the arguments received are always the directly result of un-JSON’ing the payload from the frontend. So the dict is shaped like a Customer and you can do self.current_user = Customer(**user).
Also the simple “cast” self.current_user = Customer(**user) is not so useful as soon as Customer includes some DB relationship and the form json includes nested dicts so it would be really nice to have the framework handle those things in the background
As of 0.6.5, if an event handler arg is annotated as a specific rx.Base, rx.Model or dataclass, then we will internally cast it. For models, we strip out relationship fields, and pydantic seems to handle the nested dict/model just fine.
Give it a shot on 0.6.5 and see if it works for your use case. If there are any bugs we can squash them
Ah i see, and it’s probably not feasible to load the relationship because the instance is unconnected from any session. Not sure if there’s a good way to solve this. However if you annotate the event handler arg as dict instead of the model type, you will get the original dict back with the relationships intact.