-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix defect: views's filter doesn't work in django 2.0+
- Loading branch information
1 parent
4b403a8
commit 9d88fb8
Showing
5 changed files
with
45 additions
and
15 deletions.
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
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
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,30 @@ | ||
from django import VERSION as DJANGO_VERSION | ||
from rest_framework.viewsets import ModelViewSet | ||
|
||
|
||
class CompatModelViewSet(ModelViewSet): | ||
""" | ||
A compatibility class that extends the ModelViewSet class from Django REST Framework. | ||
This class provides compatibility for different versions of Django. | ||
Attributes: | ||
filter_fields (list): | ||
A list of fields to be used for filtering the queryset. | ||
Used in Django 1.x. | ||
filterset_fields (list): | ||
Used since Django 2.x. | ||
A list of fields to be used for filtering the queryset. | ||
Methods: | ||
__init__(self, *args, **kwargs): Initializes the CompatModelViewSet instance. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
if DJANGO_VERSION < (2, 0): | ||
if not hasattr(self, 'filter_fields') and hasattr(self, 'filterset_fields'): | ||
self.filter_fields = getattr(self, 'filterset_fields', []) | ||
else: | ||
if not hasattr(self, 'filterset_fields') and hasattr(self, 'filter_fields'): | ||
self.filterset_fields = getattr(self, 'filter_fields', []) | ||