Python Django过滤器-我想对前面的查询进行排序,而不是对整个模型进行排序
例如,当我按城市筛选表时,它可以工作Python Django过滤器-我想对前面的查询进行排序,而不是对整个模型进行排序,python,django,django-rest-framework,django-views,django-templates,Python,Django,Django Rest Framework,Django Views,Django Templates,例如,当我按城市筛选表时,它可以工作 但是,如果我想使用多选菜单按日期或价格排序,django filter会按所选值而不仅仅是城市对整个模型进行排序。 我如何解决这个问题 import django_filters from django_filters import DateFilter from .models import ModelName class SortFilter(django_filters.FilterSet): SORT_BY_DATE_AND_PRIC
但是,如果我想使用多选菜单按日期或价格排序,django filter会按所选值而不仅仅是城市对整个模型进行排序。
我如何解决这个问题
import django_filters
from django_filters import DateFilter
from .models import ModelName
class SortFilter(django_filters.FilterSet):
SORT_BY_DATE_AND_PRICE = (
('date_asc', 'Date ascending'),
('date_desc', 'Date descending'),
('price_asc', 'Price ascending'),
('price_desc', 'Price descending')
)
sort = django_filters.ChoiceFilter(label='Order by:', choices=SORT_BY_DATE_AND_PRICE, method='filter_by_date_and_price')
class Meta:
model = ModelName
fields = ['city', 'destination']
def filter_by_date_and_price(self, queryset, name, value):
if value == 'date_asc':
sort = 'date'
elif value == 'date_desc':
sort = '-date'
elif value == 'price_asc':
sort = 'price'
elif value == 'pret_desc':
sort = '-price'
return queryset.order_by(sort)
Entry.objects.filter(value=“value”).filter(other_value=“other value”)我想您正在寻找类似的东西。我本打算把它作为一个评论,但我写信是为了回答,因为我的代表分数不够
您可以在过滤对象后使用点指定更多内容。