#đź”’ How do you validate and serialize data?

5 messages · Page 1 of 1 (latest)

iron drum
#

In short - I have a backend on FastAPI (i.e. snake_case is used) and frontend on React (i.e. camlCase is used) and I want that when data comes to Pydantic model - snake_case is used for validation and camlCase is used for serialization. I currently have such a model:

class Clan(Document):
    id: str = Field(default_factory=get_clan_id, pattern=cfg.regexC)

    name: str = Field(..., max_length=100)
    icon_url: Optional[str] = Field(None, max_length=255)

    owner: Link[User] = Field(...)
    premium: Premium = Field(default_factory=Premium)

    admins: list[LowUserPublic] = Field(default=[])
    leaders: list[LowUserPublic] = Field(default=[])

    created_at: int = Field(default_factory=utc_now_timestamp)

    model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True, from_attributes=True)

And such a route:

@router.get('/').
async def get_clans(
    page: int = Query(1, ge=1),
    limit: int = Query(5, ge=1, le=10),
    payload: dict = Depends(login_required)
):
    skip = (page - 1) * limit
    user_id: str = payload.get('userId')
    
    clans = await Clan.find({
        '$or': [
            {'owner.$id': user_id},
            {'leaders.id': user_id},
            {'admins.id': user_id}
        ]
    }).skip(skip).limit(limit).to_list(limit)

    If not clans:
        return JSONResponse({'clans': []})

    return JSONResponse({'clans': jsonable_encoder(
        clans, include=['id', 'name', 'icon_url', 'premium', 'created_at'], by_alias=True)})
surreal skyBOT
#

@iron drum

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

iron drum
# iron drum In short - I have a backend on **FastAPI** (i.e. `snake_case` is used) and front...

But for some reason, for the icon_url and created_at fields, nothing comes up in the response:

[
    {
        { “id”: “92fd809f-bc80-498d-915d-4cf188baa4a7”,
        { “name”: “Elon Musk”,
        “iconUrl": null,
        “premium": {
            “is_active": false,
            “until": null,
            “max_source_channel_per_guild": 10,
            “max_target_channel_per_source_channel": 5,
            “max_message_to_forward_per_source_channel": 250
        },
        { “createdAt”: 1737247104
    },
    {
        { “id”: “ade1879a-c4dd-4804-80a9-b424e9936a08”,
        { “name”: “Land of Fire”,
        “iconUrl": null,
        “premium": {
            “is_active": false,
            “until": null,
            “max_source_channel_per_guild": 10,
            “max_target_channel_per_source_channel": 5,
            “max_message_to_forward_per_source_channel": 250
        },
        { “createdAt”: 1737247104
    }
]
surreal skyBOT
#

@iron drum

Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.