Python3 pydantic model serialization

31 views Asked by At

I have the following tortoise ORM setup and corresponding pydantic models:

class Chat(models.Model):
    id = fields.UUIDField(pk=True)

    class Meta:
        table = "chat"

    class PydanticMeta:
        pass

class ChatHistory(models.Model):
    id = fields.UUIDField(pk=True)
    chat: fields.ForeignKeyRelation["Chat"] = fields.ForeignKeyRelation("models.Chat",
                                                                         index=True,
                                                                         related_name="messages")
    content: str = fields.TextField()

    class Meta:
        table = "chat_history"
        indexes = ["chat"]

    class PydanticMeta:
        exclude = ["chat"]

Chat_Pydantic = pydantic_model_creator(Chat, name="Chat")

And I have a fastapi where I want to have two endpoints, one should give back a list of all Chat with the messages field contained and one without. At least from my understanding the best way to achieve this is to add some sort of different serializers to the Chat. But how can I create N different serializers and use them in a fastapi endpoint flexibly?

0

There are 0 answers