mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 18:08:03 +03:00 
			
		
		
		
	Fix get_search_fields example (#6487)
This commit is contained in:
		
							parent
							
								
									0ab527a3df
								
							
						
					
					
						commit
						ac7b20cca2
					
				| 
						 | 
					@ -220,10 +220,13 @@ By default, the search parameter is named `'search`', but this may be overridden
 | 
				
			||||||
 | 
					
 | 
				
			||||||
To dynamically change search fields based on request content, it's possible to subclass the `SearchFilter` and override the `get_search_fields()` function. For example, the following subclass will only search on `title` if the query parameter `title_only` is in the request:
 | 
					To dynamically change search fields based on request content, it's possible to subclass the `SearchFilter` and override the `get_search_fields()` function. For example, the following subclass will only search on `title` if the query parameter `title_only` is in the request:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class CustomSearchFilter(self, view, request):
 | 
					    from rest_framework import filters
 | 
				
			||||||
        if request.query_params.get('title_only'):
 | 
					    
 | 
				
			||||||
            return ('title',)
 | 
					    class CustomSearchFilter(filters.SearchFilter):
 | 
				
			||||||
        return super(CustomSearchFilter, self).get_search_fields(view, request)
 | 
					        def get_search_fields(self, view, request):
 | 
				
			||||||
 | 
					            if request.query_params.get('title_only'):
 | 
				
			||||||
 | 
					                return ('title',)
 | 
				
			||||||
 | 
					            return super(CustomSearchFilter, self).get_search_fields(view, request)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
For more details, see the [Django documentation][search-django-admin].
 | 
					For more details, see the [Django documentation][search-django-admin].
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user