我的API端点是http://127.0.0.1:7009/apps/2/versions
我无法过滤,订购和搜索特定版本数据
models.py:
class App(models.Model):
app_id = models.CharField(max_length=40,help_text="app_id of the app",unique=True)
name=models.CharField(max_length=40,help_text="name of the app")
class AppVersion(models.Model):
app_version = models.CharField(max_length=200)
app_description = models.CharField(max_length=200,blank=True)
Views.py:
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter,SearchFilter
class AppVersionListView(mixins.ListModelMixin,mixins.CreateModelMixin,generics.GenericAPIView):
lookup_field = 'pk'
serializer_class = appVersionsSerializer
queryset = AppVersion.objects.all()
filter_backends = (DjangoFilterBackend, OrderingFilter,SearchFilter,)
filter_fields = ('id','app_version','app_description ')
ordering_fields = '__all__'
search_fields =('app_version','app_description')
serializers.py:(app_versions的类)
class appVersionsSerializer(QueryFieldsMixin,serializers.ModelSerializer):
class Meta:
model = AppVersion
fields = ('apps','app_version','app_description')
我想基于“ app_description”来过滤版本数据,
获取: http://127.0.0.1:7009/apps/2/versions?app_description = FileCommander
预期输出:
应过滤数据并返回具有该说明的结果
[
{
"apps": 2,
"app_version": "v1",
"app_description ":"FileCommander"
}]
实际输出:
[
{
"apps": 2,
"app_version": "v1",
"app_description ":"FileCommander"
},
{
"apps": 2,
"app_version": "v2",
"app_description ":"cooking"
}]
如何搜索,过滤或订购版本数据?