Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use django filters to realize data filtering

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use django filters to achieve data filtering", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use django filters to achieve data filtering" article.

The current loop is commonly used. When the default data is empty, set the default value length to take the variable length filesizeformat file size to the time that the readable slice takes from the specified location to the pointing slice datedatetime, to the specified format safe to prevent XSS attacks, and add safe to pass the tag truncatechars to display the rest.

Examples

{# format value | function #} {# if there is no value, then use the default value #}

{{bucunzai | empty default:''}}

{# take out variable length #} {{name}}-- {{name | length}} {# file size converted to readable kb automatically converted to bm, g, tb#}

File size {{file_size | filesizeformat}}

{# slice from specified position to finger location, for example: bit 3 to-2 bit #}

Slice: {{slice_str | slice:'3:-2'}}

{# the time of fetching the datetime to the specified format #}

Formatting: {{now | date:'Y-m-d Havaniriza s'}}

{# if the backend content contains tags, then add safe to escape (prevent users from cheating directly with script tags) to prevent XSS attacks #}

{{h_html | safe}}

{# the summary is displayed only one paragraph, after the specified length. Example: 120 characters #}

Long text: {{p_str | truncatechars:12}}

1. View

Class UserView (ListAPIView): "" user list "" queryset = User.objects.all () serializer_class = UserSerializer filter_backends = (DjangoFilterBackend,) filter_class = UserMonthFilter # specify the filter class

2. Filter class

Class RobotFilter (django_filters.FilterSet): # use filtering: URL?created_start_time=2020_01-20&created_end_time=2020_01-21 robot_id = django_filters.CharFilter (field_name='id') machine_id = django_filters.CharFilter (field_name='machine_id') city = django_filters.CharFilter (field_name='city') # lookup_expr (optional) is the judgment condition, field_name (required) is the model class attribute Created_time query string created_time= django_filters.CharFilter (field_name='created_at', lookup_expr='startswith') created_start_time = django_filters.DateTimeFilter (field_name='created_at', lookup_expr='gt') created_end_time = django_filters.DateTimeFilter (field_name='created_at', lookup_expr='lt') problem_isnull = django_filters.BooleanFilter (field_name='problem' Lookup_expr='isnull') name = django_filters.CharFilter (lookup_expr='iexact') # iexact for exact match, and ignore case author = django_filters.CharFilter (lookup_expr='icontains') # icontains for fuzzy query (inclusive) And ignore the case price = django_filters.NumberFilter (look_expr='exact') # exact to indicate an exact match task_res_state = django_filters.CharFilter (method= "get_task_res_state") def get_task_res_state (self, queryset, * arg): if str (arg [1]) = "0": # arg [1] = ('task_res_state',' 0') task_res = (1) 2, 3) else: task_res = (0,4,5,6) print (task_res) queryset = queryset.filter (task_res__in=task_res) return queryset class Meta: model = Robot fields = ['robot_id',' machine_id', "city", "created_start_time", "created_end_time", 'created_time' 'firmware_version',' state', "robot_type", "hardware_version", "exist_map", 'task_res_state'] are the contents of this article on "how to implement data filtering with django filters" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report