-
I have a state which contains a list of objects. To illustrate I simplified my code to the bare minimum:
When I substitute hello_name for just name it works fine. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
@hpharmsen The It looks like Pydantic supports computed_fields from their v2.0 but We are still using 1.10 in Reflex. Here's a way to do this currently by overriding the import reflex as rx
class Person(rx.Base):
name: str
@property
def hello_name(self) -> str:
return "Hello " + self.name
def dict(self, **kwargs):
return super().dict(**kwargs) | {"hello_name": self.hello_name}
class State(rx.State):
count: int = 5
persons: list[Person] = [Person(name="Alice"), Person(name="Bob")]
@rx.page()
def index():
return rx.foreach(
State.persons, lambda person: rx.html("<p>" + person.hello_name + "</p>")
)
app = rx.App() |
Beta Was this translation helpful? Give feedback.
-
Related to #2429, reflex could automatically serialize those properties without the |
Beta Was this translation helpful? Give feedback.
@hpharmsen The
@rx.var
decorator only works within state classes (not within the a Base class as you have it here). You want to use the@property
decorator that's builtin to Python instead.It looks like Pydantic supports computed_fields from their v2.0 but We are still using 1.10 in Reflex.
Here's a way to do this currently by overriding the
dict
method to make sure the property gets serialized: