-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from rkshaon/171-publisher-apis
done: #124 - search API
- Loading branch information
Showing
6 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from rest_framework.filters import BaseFilterBackend | ||
|
||
|
||
class SearchFilter(BaseFilterBackend): | ||
""" | ||
A custom search filter that allows more flexible query handling. | ||
""" | ||
|
||
def get_search_fields(self, view): | ||
""" | ||
Returns the search fields defined on the view. | ||
""" | ||
return getattr(view, 'search_fields', None) | ||
|
||
def filter_queryset(self, request, queryset, view): | ||
""" | ||
Perform custom filtering based on search fields and query parameters. | ||
""" | ||
search_fields = self.get_search_fields(view) | ||
search_param = request.query_params.get( | ||
'search') # Default query param is `search` | ||
|
||
if not search_fields or not search_param: | ||
return queryset # No search fields or query parameter, return unfiltered queryset # noqa | ||
|
||
# Build a query dynamically | ||
from django.db.models import Q | ||
query = Q() | ||
for field in search_fields: | ||
# Case-insensitive search | ||
query |= Q(**{f"{field}__icontains": search_param}) | ||
|
||
return queryset.filter(query) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
backend/publisher_api/migrations/0003_alter_publisher_name.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.1.1 on 2024-11-28 20:25 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('publisher_api', '0002_publisher_added_by_publisher_added_date_time_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='publisher', | ||
name='name', | ||
field=models.CharField(db_index=True, max_length=255), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters