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.
3 replies
-
Related to #2429, reflex could automatically serialize those properties without the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a state which contains a list of objects.
How do I access a computed var in one of these sub objects?
To illustrate I simplified my code to the bare minimum:
class Person(rx.Base):
name: str
@rx.var
def hello_name(self) -> str:
return "Hello " + self.name
class State(rx.State):
count: int = 5
persons: list[Person] = [Person(name='Alice'), Person(name='Bob')]
def index():
return rx.foreach(
State.persons,
lambda person: rx.html('<p>' + person.hello_name + '</p>')
)
When I substitute hello_name for just name it works fine.
Somehow I don't get the concept.
Beta Was this translation helpful? Give feedback.
All reactions