-
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 #18 from awaisdar001/get-distinc-records
Get Distinct records from the API
- Loading branch information
Showing
3 changed files
with
49 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,43 @@ | ||
import django_filters | ||
from django_trips.models import Trip | ||
import django_filters as filters | ||
from django_trips.models import Trip, Location | ||
from django.db.models import Q | ||
|
||
|
||
class TripFilter(django_filters.FilterSet): | ||
name = django_filters.CharFilter(field_name='name', lookup_expr=('icontains')) | ||
duration = django_filters.NumberFilter(field_name='duration', lookup_expr=('gte')) | ||
price = django_filters.NumberFilter(field_name='trip_schedule__price', method='filter_price') | ||
destination = django_filters.CharFilter(field_name='destination', method='filter_price') | ||
class TripFilter(filters.FilterSet): | ||
name = filters.CharFilter(field_name='name', lookup_expr=('icontains')) | ||
destination = filters.BaseInFilter(field_name="destination__slug") | ||
duration_from = filters.NumberFilter(field_name='duration', lookup_expr=('gte')) | ||
duration_to = filters.NumberFilter(field_name='duration', lookup_expr=('lte')) | ||
price_from = filters.NumberFilter( | ||
label="Price From", | ||
field_name='trip_schedule__price', | ||
method='filter_from' | ||
) | ||
price_to = filters.NumberFilter( | ||
label='Price To', | ||
field_name='trip_schedule__price', | ||
method='filter_to' | ||
) | ||
|
||
from_date = django_filters.DateFilter( | ||
label="Trip From Date", | ||
date_from = filters.DateFilter( | ||
label="Trip Date From", | ||
field_name='trip_schedule__date_from', | ||
method='filter_from_date', | ||
method='filter_from', | ||
) | ||
to_date = django_filters.DateFilter( | ||
label="Trip To Date", | ||
date_to = filters.DateFilter( | ||
label="Trip Date To", | ||
field_name='trip_schedule__date_from', | ||
method='filter_to_date', | ||
method='filter_to', | ||
) | ||
|
||
def filter_from_date(self, queryset, name, value): | ||
def filter_from(self, queryset, name, value): | ||
lookup = '__'.join([name, 'gte']) | ||
return queryset.filter(**{lookup: value}) | ||
return queryset.filter(**{lookup: value}).distinct() | ||
|
||
def filter_to_date(self, queryset, name, value): | ||
def filter_to(self, queryset, name, value): | ||
lookup = '__'.join([name, 'lte']) | ||
return queryset.filter(**{lookup: value}) | ||
|
||
def filter_price(self, queryset, name, value): | ||
lookup = '__'.join([name, 'gte']) | ||
return queryset.filter(**{lookup: value}) | ||
return queryset.filter(**{lookup: value}).distinct() | ||
|
||
class Meta: | ||
model = Trip | ||
fields = ('name', 'price') | ||
fields = ('destination',) |
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