Let’s get back to the topic of Python development technology from today, after all, this is what we eat.
Today we will talk about a package called Django-filter
that I commonly use in daily development. This package is very convenient for filtering and developing our daily Django development work. We only need to configure a few lines of code to complete our complex of screening.
I mentioned it when I introduced Swagger in the article “Super easy-to-use API tool-Swagger”. Especially for the XXX system management page, it is not very convenient to process. Let me explain in detail below.
Installation configuration
The installation of this tool is very simple through pip.
pip install django-filter
Here I would like to explain the latest django-filter installed here. By default, the new version requires Python 3. If our environment is Python 2.7, we need to install the django-filter 1.1.0 version. In addition, this package depends on the djangorestframework framework. I will refer to it below. DRF.
After installation, we add INSTALLED_APPS
ini Copy code # settings.py INSTALLED_APPS = [ ... 'rest_framework', 'django_filters', ] REST_FRAMEWORK = { 'DEFAULT_FILTER_BACKENDS': ( 'django_filters.rest_framework.DjangoFilterBackend', ... ), }
Here django-filter is used as the default backends, which can also be written in the viewset, see below.
Use
When using it, we need to create a FilterSet class to filter. Without further ado, let’s look at a code.
javascript Copy code from rest_framework import generics from django_filters import rest_framework as filters from myapp import Product class ProductFilter(filters.FilterSet): min_price = filters.NumberFilter(field_name="price", lookup_expr='gte') max_price = filters.NumberFilter(field_name="price", lookup_expr='lte') class Meta: model=Product fields = ['category', 'in_stock', 'min_price', 'max_price']
The above code shows our price range filtering. In practice, we can have more filtering and display methods.
ini Copy code class ProductFilter(django_filters.rest_framework.FilterSet): class Meta: model=Product fields = { 'order_id': ['exact'], 'status': ['in'], 'created_at': ['range'], 'card__company_name': ['contains'], 'card__email': ['exact'], } class ProductList(generics.ListAPIView): queryset = Product.objects.all() serializer_class = ProductSerializer filter_backends = (filters.DjangoFilterBackend,) # We don’t need to configure the settings here. # filterset_class = ProductFilter # django-filter 2.1, python3 filter_class = ProductFilter #django-filter 1.1, python2.7
Above, I show the filtering situation of django-filter from several dimensions such as time range selection, precise filtering, and inclusion, which can satisfy most filtering situations. In addition, I marked that filterset_class is written after Django-filter 1.1, and filter_class is written after Django-filter 1.1. For versions before 1.1, everyone needs to pay attention here.
Effect
As you can see from the above code, by integrating Django-filter into DRF, daily filtering operations are very convenient and effective, which greatly improves our back-end efficiency. You can develop and debug by yourself and deliver complete interfaces to front-end students, making everyone more happy to connect.
Combined with the Swagger I mentioned earlier, the rendering is as follows.
Digression
In this era of rapidly growing technology, programming is like a ticket to a world of infinite possibilities for many people. Among the star lineup of programming languages, Python is like the dominant superstar. With its concise and easy-to-understand syntax and powerful functions, Python stands out and becomes one of the hottest programming languages in the world.
The rapid rise of Python is extremely beneficial to the entire industry, but “There are many popular people and not many people
“, which has led to a lot of criticism, but it still cannot stop its popularity. development momentum.
If you are interested in Python and want to learn Python, here I would like to share with you a Complete set of Python learning materials, which I compiled during my own study. I hope it can help you, let’s work together!
Friends in need can click the link below to get it for free or Scan the QR code below to get it for free
CSDN Gift Package: Free sharing of the most complete “Python learning materials” on the entire network(safe link, click with confidence )
?
1Getting started with zero basics
① Learning route
For students who have never been exposed to Python, we have prepared a detailed Learning and Growth Roadmap for you. It can be said to be the most scientific and systematic learning route. You can follow the above knowledge points to find corresponding learning resources to ensure that you learn more comprehensively.
② Route corresponding learning video
There are also many learning videos suitable for beginners. With these videos, you can easily get started with Python~
③Exercise questions
After each video lesson, there are corresponding exercises to test your learning results haha!
2Domestic and foreign Python books and documents
① Documents and books
3Python toolkit + project source code collection
①Python toolkit
The commonly used development software for learning Python is here! Each one has a detailed installation tutorial to ensure you can install it successfully!
②Python practical case
Optical theory is useless. You must learn to type code along with it and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases. 100+ practical case source codes are waiting for you!
③Python mini game source code
If you feel that the practical cases above are a bit boring, you can try writing your own mini-game in Python to add a little fun to your learning process!
4Python interview questions
After we learn Python, we can go out and find a job if we have the skills! The following interview questions are all from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and Alibaba bosses have given authoritative answers. I believe everyone can find a satisfactory job after reviewing this set of interview materials.
5Python part-time channel
Moreover, after learning Python, you can also take orders and make money on major part-time platforms. I have compiled various part-time channels + part-time precautions + how to communicate with customers into documents.
All the above information , if friends need it, you can scan the QR code below to get it for free
?