FastAPI and Pydantic - URL Query Parameters for Filtering

preview_player
Показать описание

In this video, we'll see how to define Query Parameters in our API routes for filtering data.

We'll see how to combine this with Pydantic model functionality and how to add multiple query parameters to a list route - this is useful for searching and filtering in an API

☕️ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲:
To support the channel and encourage new videos, please consider buying me a coffee here:

▶️ Full Playlist:

𝗦𝗼𝗰𝗶𝗮𝗹 𝗠𝗲𝗱𝗶𝗮:

📚 𝗙𝘂𝗿𝘁𝗵𝗲𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝗱 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻:

#python #fastapi #webdevelopment
Рекомендации по теме
Комментарии
Автор

Straight to the point explanation good job !

hamzaelmhoujab
Автор

Your content is a gold mate. I hope you keep it going as this is really helpful for someone like me. Thanks for your time and effort. 👍

graczew
Автор

The video is great, but unfortunately there is a significant mistake in the code. You should not use bool query parameters in if conditions such as `if has_albums:`. The reason for this is that if you want to query metal bands that don't have any albums, you would type in the url and in your example you would expect an empty list (there is no metal band with no albums in your list), but you end up getting the band Black Sabbath, which is not expected because this band has 3 albums. This is because `has_albums=false` is treated as False in the condition `if has_albums:`, so the bans are never filtered by this parameter. You can't remove the condition completely either, because you will always filter bands with albums. Correct me if I'm wrong, but to fix this IMO the has_albums parameter should be None by default, and the condition should look like this: `if has_albums is not None:` and the filtering like this `band_list = [b for b in band_list if bool(b.albums) is has_albums]`. Maybe you have some better solution for this problem? I think it would be good to give such an example in the video, because this is a really common mistake in building Rest APIs. Nevertheless, I really appreciate your work and really enjoyed the whole series!

Szczurek
Автор

In this part of the video at 5:30, I get the same result when we write the function as 'async def bands(genre: GenreURLChoice = None)' instead of 'async def bands(genre: GenreURLChoice | None = None)'. What's the difference between these?

Reb-
Автор

How about for searching bands with more than one genre? FastAPI might handle this well, but in Django, GET ends up overwriting (Django-filter handles it fine). I.e genre=rock&genre=rap : get[‘genre’] will return rap. It seems to just be how Django does it, it’s written in the docs, but is there a better way to handle this without using Django-filter? Keep up the good work!

ryansanta
Автор

When viewing the json in the browser, how did you get it pretty printed like that? Is that a fastapi thing or a chrome thing?

infernape