Skip to content

How to set a relationship count in Response Model / Schema #652

Answered by jplacht
nadavof asked this question in Questions
Discussion options

You must be logged in to vote

I would also recommend not solving this in the database, but with Pydantic by upgrading to the latest SQLModel that supports Pydantic V2. You would then be able to define a model that is transforming your database ParentRead utilizing computed fields with some minor adjustments.

Below an example for a computed field if the Read model inherits the list of children:

from pydantic import computed_field

class ParentRead(ParentBase):
    @computed_field
    @property
    def children_counter(self) -> int:
        return len(self.children)

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@nadavof
Comment options

@jd-solanki
Comment options

@nadavof
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
3 participants