我正在使用django rest框架,我想用自定义方法订购。
所以,当我打电话给这个网址时,例如:http://127.0.0.1:8000/api/sets/?ordering=partMissing
可以调用自定义方法,因为我希望通过代码片段丢失部分的数量。我计算了多对多领域中部分数量的总和。
提前感谢您的回答。
答案 0 :(得分:5)
我有非常简单的POC,应该允许你实现更复杂的解决方案。
views.py
from rest_framework import viewsets
from ordering_test.models import Test
from ordering_test.ordering import MyCustomOrdering
from ordering_test.serializers import TestSerializer
class TestViewSet(viewsets.ModelViewSet):
queryset = Test.objects.all()
serializer_class = TestSerializer
filter_backends = (MyCustomOrdering,)
ordering.py
from rest_framework.filters import OrderingFilter
class MyCustomOrdering(OrderingFilter):
allowed_custom_filters = ['testMethod']
def get_ordering(self, request, queryset, view):
"""
Ordering is set by a comma delimited ?ordering=... query parameter.
The `ordering` query parameter can be overridden by setting
the `ordering_param` value on the OrderingFilter or by
specifying an `ORDERING_PARAM` value in the API settings.
"""
params = request.query_params.get(self.ordering_param)
if params:
fields = [param.strip() for param in params.split(',')]
# care with that - this will alow only custom ordering!
ordering = [f for f in fields if f in self.allowed_custom_filters]
if ordering:
return ordering
# No ordering was included, or all the ordering fields were invalid
return self.get_default_ordering(view)
def filter_queryset(self, request, queryset, view):
ordering = self.get_ordering(request, queryset, view)
if ordering:
# implement a custom ordering here
ordering = ['-id']
if ordering:
return queryset.order_by(*ordering)
return queryset
models.py 和 serializers.py 很简单,但会把它放在这里:
models.py
来自django.db导入模型
class Test(models.Model):
test = models.CharField(max_length=120)
test1 = models.CharField(max_length=120)
serializers.py
from rest_framework import serializers
from ordering_test.models import Test
class TestSerializer(serializers.ModelSerializer):
class Meta:
model = Test
fields = ('test', 'test1')
快乐的编码!
答案 1 :(得分:0)
我认为opalczynski解决方案更简单的方法是为自定义订购引入一个新领域:
6
-80 800 -8000 -800 80 8000
然后,您可以轻松地按自己想要的任何字段进行排序,例如:max is 4253776 min is 4253776
。
在我的示例中,我使用了固定的模型字段,但是您可以在from django import forms
import django_filters
from rest_framework import serializers
from .models import MyModel
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = ('field1',)
class CustomOrderingFilter(django_filters.FilterSet):
order_by = django_filters.BooleanFilter(
widget=forms.HiddenInput(),
method='filter_order_by',
)
class Meta:
model = MyModel
fields = [
'order_by'
]
def filter_order_by(self, queryset, name, value):
if value:
return self.Meta.model.objects.filter(
id__in=queryset.values_list('id', flat=True)
).order_by(value)
return queryset
class TestViewSet(viewsets.ModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
filter_class = CustomOrderingFilter
上的example.com/api/mymodel/?order_by=partMissing
方法中更改订购方式。只需将其更改为所需的逻辑即可,但是请确保使用filter_order_by
以确保正在使用设置的其他过滤器。