Django-Filter - Filtering Django Querysets, Class-Based Views and REST Framework Integration

  Рет қаралды 31,251

BugBytes

BugBytes

Күн бұрын

In this video, we'll learn about the very useful Django Filter package, which allows you to extract filtering logic for your querysets into dedicated classes, called FilterSets.
We'll go over an example of filtering a list of books, using a FilterSet, and will demonstrate how to use Django-Filter with functional and class-based views.
We'll also demonstrate django-filter integration with Django REST Framework.
📌 𝗖𝗵𝗮𝗽𝘁𝗲𝗿𝘀:
00:00 Intro
00:47 Building Django Models
03:21 Creating Form to Filter Books
06:23 Adding Books to Database with Management Command
08:17 Filtering Books from GET request parameters
10:34 Installing django-filter
10:46 Creating a FilterSet class
14:47 Customizing FilterSet lookup expressions
17:30 Defining a RangeFilter for the price field
18:22 Using FilterSets in Class-Based Views
21:51 Integrating FilterSets with Django REST Framework
☕️ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲:
To support the channel and encourage new videos, please consider buying me a coffee here:
ko-fi.com/bugbytes
▶️ Full Playlist:
• Django Primers
𝗦𝗼𝗰𝗶𝗮𝗹 𝗠𝗲𝗱𝗶𝗮:
📖 Blog: bugbytes.io/posts/
👾 Github: github.com/bugbytes-io/
🐦 Twitter: / bugbytesio
📚 𝗙𝘂𝗿𝘁𝗵𝗲𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝗱 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻:
django-filter docs: django-filter.readthedocs.io/...
django-filter FilterSet: django-filter.readthedocs.io/...
Django choices: docs.djangoproject.com/en/4.1...
#django #python #webpack #frontend

Пікірлер: 100
@seydinaoumarsamabaly1806
@seydinaoumarsamabaly1806 Жыл бұрын
Always surprising with content of quality. Thank you for all mate.
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thank you!
@Davidkiania
@Davidkiania Жыл бұрын
I really really love this channel ... I have learned in 15 mins what could have taken me an hour in other videos and your explanations are excellent and concise. Thank you.
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thank you very much, I'm glad you're enjoying the videos!
@garzuze
@garzuze 5 ай бұрын
Straight to the point and very informational. Loved it!
@bugbytes3923
@bugbytes3923 5 ай бұрын
Thanks a lot!
@metalhoe
@metalhoe Жыл бұрын
Thank you, this tutorial helped me very much. I was basing my stuff on another django tutorial of filter search but it was outdated, started new with this one and everything works. All the best 😃
@bugbytes3923
@bugbytes3923 Жыл бұрын
Glad to hear that, thanks a lot!
@helloh6
@helloh6 Жыл бұрын
I needed this video so much! Thanks so much for making all of your videos!
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thanks Ben - glad it was useful!
@helloh6
@helloh6 Жыл бұрын
@@bugbytes3923 No problem. If you ever need a video or blog idea, I've been trying to find a good way to use HTMX to display messages from the Django Message system as they are created, but haven't found a clean way to do it yet. Seems like the sort of thing I normally learn from your videos haha!
@arunima2916
@arunima2916 Жыл бұрын
covering all the related features nicely with ease, great job, expecting something for the full text search also, thank u!!!
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thank you! I'll have a look into that.
@johnchiverton9375
@johnchiverton9375 Жыл бұрын
Great video. Really clearly explained how and why. Looking forward to watching more of your channel.
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thanks a lot, that's great to hear!
@samwang8500
@samwang8500 Жыл бұрын
I appreciate your so clear and awesome tutorial!
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thank you Sam. Appreciate the comment!
@frankyang6943
@frankyang6943 Жыл бұрын
Thank you for your example. this is very helpful to me!
@bugbytes3923
@bugbytes3923 Жыл бұрын
You are welcome! I’m glad it was useful!
@calimhiro
@calimhiro 6 ай бұрын
very great explanations, simple and clear! I love your videos!
@bugbytes3923
@bugbytes3923 6 ай бұрын
Thanks a lot!
@axelb.2006
@axelb.2006 8 ай бұрын
For me, the best Channel with a big C. Thanks
@bugbytes3923
@bugbytes3923 8 ай бұрын
Thanks so much!
8 ай бұрын
As always, your videos are super useful
@bugbytes3923
@bugbytes3923 8 ай бұрын
Thanks a lot!!
@lalomores4686
@lalomores4686 9 ай бұрын
Excellent explanation. Thanks!
@bugbytes3923
@bugbytes3923 9 ай бұрын
Thank you!
@repotranstech9614
@repotranstech9614 Жыл бұрын
Thanks for this amazing tutorial 👍.
@bugbytes3923
@bugbytes3923 Жыл бұрын
You're welcome, cheers!
@Developer_python_
@Developer_python_ 4 ай бұрын
You're cool!!!! I just have thought about rest and you start to talk about it))))
@bugbytes3923
@bugbytes3923 4 ай бұрын
Thanks a lot, appreciate it!
@ravikiranp7532
@ravikiranp7532 10 ай бұрын
Damn! This is a gold mine!
@bugbytes3923
@bugbytes3923 10 ай бұрын
Thanks!
@AmoahDevLabs
@AmoahDevLabs Жыл бұрын
Wonderful. This is really incredible. Keep it up man. Expecting more from you on Django-Unicorn
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thanks a lot man. will do!
@user-xd9js8ex8l
@user-xd9js8ex8l 4 ай бұрын
Nice video man, thank you!
@bugbytes3923
@bugbytes3923 4 ай бұрын
Thanks a lot, appreciate it!
@h3ckphy246
@h3ckphy246 Жыл бұрын
I would put your video on the docs instead of official documentation. Thank you.
@bugbytes3923
@bugbytes3923 Жыл бұрын
Haha thank you, kind words!
@user-ek7ur3qi6m
@user-ek7ur3qi6m 11 ай бұрын
After 3 days of trying, by this video my problem is solved :D
@bugbytes3923
@bugbytes3923 11 ай бұрын
Awesome, glad to hear the video helped! 😄
@keinermendoza4631
@keinermendoza4631 3 ай бұрын
What an excellent explanation.
@bugbytes3923
@bugbytes3923 3 ай бұрын
Thanks a lot!
@odoolens1190
@odoolens1190 Жыл бұрын
Thanks for this!
@bugbytes3923
@bugbytes3923 Жыл бұрын
You’re welcome!
@user-wq5ms1zc7m
@user-wq5ms1zc7m Жыл бұрын
It was so helpful and useful and also taught simply👏 Thanks a lot 🙏😊
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thank you very much, I’m glad it was useful! 👍
@MrThestormguy
@MrThestormguy 7 ай бұрын
You are such a boss.
@bugbytes3923
@bugbytes3923 7 ай бұрын
Thanks a lot!
@eugene_mountainland
@eugene_mountainland Жыл бұрын
great stuff, thank you)
@bugbytes3923
@bugbytes3923 Жыл бұрын
You're welcome! Thanks for watching.
@developer-pf2fl
@developer-pf2fl Жыл бұрын
it's so helpful . Thank You so Much (Subscribed)
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thanks a lot! Glad to hear that!
@prajjwaldangal2022
@prajjwaldangal2022 3 ай бұрын
really clear explanation
@bugbytes3923
@bugbytes3923 3 ай бұрын
Thanks a lot!
@laszlo.laszlo
@laszlo.laszlo Жыл бұрын
Very elegant solutions. Thank you!
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thank you very much!
@markomwandasi1447
@markomwandasi1447 2 ай бұрын
This is good stuff, Thanks
@bugbytes3923
@bugbytes3923 2 ай бұрын
Thanks a lot!
@lotharhansen4170
@lotharhansen4170 Жыл бұрын
Thank you!
@bugbytes3923
@bugbytes3923 Жыл бұрын
you're welcome - thank you for watching! :)
@cyonii8683
@cyonii8683 Жыл бұрын
Great tutor
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thank you!
@svink312
@svink312 Жыл бұрын
How to style the dropdown filter to bootstraps class (or any class) e.g. model-form. Some use Crispy Forms but it would be good to have a way to add a html class. For forms it's straight forward but for the filter generated fields+buttons I haven't manage to do it. Any tips/advice? Great video thanks for posting keep it up.
@ilyosbekkarshiboyev7134
@ilyosbekkarshiboyev7134 11 ай бұрын
Thank you
@bugbytes3923
@bugbytes3923 11 ай бұрын
You're welcome, thanks for watching!
@avindafbrz17
@avindafbrz17 Жыл бұрын
thank you it was so cool and helpful. but i want to ask, i was use django admin for add the product, but the product didnt show up on the website, how to fix it?
@torbjornsvard5771
@torbjornsvard5771 Жыл бұрын
Great content!. Thanks for all your high Q vids. Is it possible to search for, let's say, multiple genres?
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thanks a lot, appreciated! Yes it is - I've just recorded a video that will show how to filter on multiple values. Will release this in the next few days after editing ;)
@AshokSharma-hk1jc
@AshokSharma-hk1jc 2 ай бұрын
Hi, one confusion is that when we should specify fields in the meta class and when we can skip still confused.
@fikkaaps
@fikkaaps Жыл бұрын
hey can u help me to remove the form label? thanks😊
@AshokSharma-hk1jc
@AshokSharma-hk1jc 2 ай бұрын
Hi, One confusion that when we should specify a field in Meta class and when we can skip.
@sharifahmed1925
@sharifahmed1925 5 ай бұрын
is there a repo for this code?
@maathias9
@maathias9 3 ай бұрын
Grate video! Let's say you want a COUNT value in the bottom of the table (or other cases you might want SUM for other purposes). Is that possible while using django-filters or is there another way to implement that with the features that comes with django-filters? I've been reading through the docs but can't seem to find a way while using django-filters. Thanks for a great channel :)
@maathias9
@maathias9 3 ай бұрын
Found solution (example) If anyone else needed: In views .py - book_filter = BookFilter(request.GET, queryset=Book.objects.all()) books = book_filter.qs # Count of the books book_count = books.count() -- Include book_count in the context, then in your template include {{book_count}} where needed (maybe create a new from your table)
@antoinebuldo6356
@antoinebuldo6356 5 ай бұрын
For me...a Big newbee its going to fast...can you help me to implement this in my crm that i created to follow my clients?
@michaelwalker5950
@michaelwalker5950 Жыл бұрын
Well presented and easy to understand. I've noticed all the videos on using Django-Filter uses objects.all() to display all database records on the page on render. What if there are 1000s of records that you don't want all rendered, but only the subset displayed after filtering?
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thank you Michael. To display a subset, you can use Django Pagination utilities to page the queryset that's returned after the filtering. Could fire up a quick video on this topic, if you'd like.
@michaelwalker5950
@michaelwalker5950 Жыл бұрын
@@bugbytes3923 Thank you for the quick response. I actually figured it out about an hour after asking. I'm using Django-Filters and checking the dictionary if any filters are populated. If yes, display the filtered rows. If no, display nothing. So far seems to be working
@evgeniigoncharov9507
@evgeniigoncharov9507 Жыл бұрын
top
@bugbytes3923
@bugbytes3923 Жыл бұрын
Thanks!
@billcooper8625
@billcooper8625 5 ай бұрын
Would this work with fk? For instance, say you have a class table,student table, & student grades table in the db. In the view, you display students in a table but then with Django-filter could you apply filtering back to the grades for each student to filter the table in the view based on students with certain grades? For instance, show me student in class A and B with Grades of b or higher….
@maathias9
@maathias9 Ай бұрын
When you initially load the page, all the books gets displayed in the template. But how would you manage to have the table empty when you load the page? And only fetch the books when you filter? And if the "name" filter is empty and you click search, it then would search the entire db and fetch all the books? Thanks for your work and channel 🙏🏻
@bugbytes3923
@bugbytes3923 Ай бұрын
Thanks for the comment! Will try and answer. For an initially empty table, you can load no books initally with: Book.objects.none() as the queryset. When you apply the filter, you can pass the params to the Book.objects.filter() function. You can prevent submitting the form with an empty name filter by adding a minimum length to the field. developer.mozilla.org/en-US/docs/Web/HTML/Attributes/minlength Note: a full-table scan over all books will occur no matter what you search for (empty string, or a certain text) unless you have an index on the Book's "name" field. Adding an index would allow much more efficient filtering over a large table.
@maathias9
@maathias9 Ай бұрын
@@bugbytes3923 Thank you for your answer. Understood how to tweak my current code a little bit. (wasn't too hard after some clarification) Again, thank you for your work💪🏼
@TomislavMiletic
@TomislavMiletic 11 ай бұрын
Thank you! Can you please explain why adding a content field inside of Meta class doesn't work, but adding it above does? This: import django_filters from .models import UserQuestionForLesson class QuestionsFilter(django_filters.FilterSet): content = django_filters.CharFilter(lookup_expr='icontains') class Meta: model = UserQuestionForLesson fields = [ 'is_answered', ] It is just an ordinary CharField where user types in a question for a lesson.
@bugbytes3923
@bugbytes3923 11 ай бұрын
Hi Tomislav. The content field you've defined should also work if added to the "fields" list in the Meta class (as longs as it exists as a field on your model). Does that not work? One caveat: for a CharFilter, I believe the default lookup is "exact", so there would have to be an exact match between the submitted content, and what's stored in the database for the "content" field
@TomislavMiletic
@TomislavMiletic 11 ай бұрын
@@bugbytes3923 that was fast, thank you. Okay so adding a content field into Meta class doesn't work. Yes, there is a "content" charfield in the Question module, and it works if being added above the Meta class, and i used icontains, it is working. Just wanted to ask because i followed the tutorial and didn't understand why it is not working.
@bugbytes3923
@bugbytes3923 11 ай бұрын
@@TomislavMiletic I think because the default lookup is “exact”, that may be why it doesn’t work when it’s only added to Meta.fields? It works above because you override the default and use “icontains” - maybe this is the issue?
@TomislavMiletic
@TomislavMiletic 11 ай бұрын
@@bugbytes3923 i will try that but that won't work for me because i don't want users to have to type the whole thing 😕 Thank you very much!
@bugbytes3923
@bugbytes3923 11 ай бұрын
@@TomislavMiletic If you want to keep the field in Meta, you can use the second of the two approaches (classes) in the code block at this link: django-filter.readthedocs.io/en/stable/ref/filterset.html#declaring-filterable-fields You can specify not only the field, but also the lookup, directly in the Meta class. Just use a dictionary, rather than a list, with dictionary values being the lookup for that field.
@janisrozenbergs4226
@janisrozenbergs4226 7 ай бұрын
Great video But I am having an issue that I get a 404 every time I enter a search any suggestions for this? can share my repository if necessary :)
@bugbytes3923
@bugbytes3923 7 ай бұрын
Feel free to share, sure! I'll have a look.
@devopsliveuz
@devopsliveuz Жыл бұрын
source code pls?
@zackmcnelly
@zackmcnelly Жыл бұрын
HTMX Filtering Video?
@bugbytes3923
@bugbytes3923 Жыл бұрын
I'll do a very quick demo of this in my next video!
@amine63404
@amine63404 5 ай бұрын
Within your list, you are missing "Brave New World"
@bugbytes3923
@bugbytes3923 5 ай бұрын
Excellent book!
@elijahgathanga9831
@elijahgathanga9831 Жыл бұрын
Well, Its a good tutorial but i don't think someone who wants to understand the concepts or how it works will get it from here.
@bugbytes3923
@bugbytes3923 Жыл бұрын
I would suggest to read the django-filter docs and work through an example on there in order to get the concepts better. Sorry this video was not useful for that!
Easily Convert Django Function Based Views To Class Based Views
11:58
ОСКАР vs БАДАБУМЧИК БОЙ!  УВЕЗЛИ на СКОРОЙ!
13:45
Бадабумчик
Рет қаралды 6 МЛН
Самый Молодой Актёр Без Оскара 😂
00:13
Глеб Рандалайнен
Рет қаралды 5 МЛН
Mom's Unique Approach to Teaching Kids Hygiene #shorts
00:16
Fabiosa Stories
Рет қаралды 12 МЛН
What it feels like cleaning up after a toddler.
00:40
Daniel LaBelle
Рет қаралды 61 МЛН
You don't need a frontend framework
15:45
Andrew Schmelyun
Рет қаралды 113 М.
Бекенд на Django, Урок 3: Filters, Search, Ordering
29:31
Senior Pomidor Developer
Рет қаралды 29 М.
Django Rest Framework for Beginners - Simple CRUD API
14:22
John Watson Rooney
Рет қаралды 47 М.
Django Rest Framework Series - Filters and Search features  - Part-6
38:41
How to Write Complicated Queries in Django With F Expressions
10:44
Pretty Printed
Рет қаралды 16 М.
Search Filtering and Ordering (Django Rest framework)
9:39
CodingWithMitch
Рет қаралды 48 М.
ОСКАР vs БАДАБУМЧИК БОЙ!  УВЕЗЛИ на СКОРОЙ!
13:45
Бадабумчик
Рет қаралды 6 МЛН