django - 来自客户端的请求以数组形式出现,如何使用 Django REST 框架过滤器处理它
问题描述
正如我在标题中提到的,客户端过滤器请求以这种模式从制表数据表到休息框架后端。
/?page=1&size=10&filters[0][field]=q&filters[0][type]=like&filters[0][value]=something
我已经尝试过这种方式,但它不起作用
class MemberListSerializerView(generics.ListAPIView):
model = Membership
serializer_class = MemberSerializer
pagination_class = CustomPagination
def get_queryset(self):
queryset = Membership.objects.members()
query = self.request.query_params.get('filters[value][0]', None)
if query is not None:
queryset = queryset.filter(
Q(user__username__icontains=query) |
Q(user__first_name__icontains=query) |
Q(user__last_name__icontains=query)
)
return queryset
解决方案
Tabulator 以这种方式发送 ajax 请求,因为它是在 PHP 请求中表示数组数据的标准方式。
如果您想以不同的方式构造请求,您可以使用ajaxURLGenerator函数以您喜欢的任何方式构造请求参数:
var table = new Tabulator("#example-table", {
ajaxURLGenerator:function(url, config, params){
//url - the url from the ajaxURL property or setData function
//config - the request config object from the ajaxConfig property
//params - the params object from the ajaxParams property, this will also include any pagination, filter and sorting properties based on table setup
//return request url
return url + "?params=" + encodeURI(JSON.stringify(params)); //encode parameters as a json object
},
});
有关完整的详细信息,请查看Ajax 文档
推荐阅读
- android - 如何为 Generate Signed Bundle / APK 操作运行单元测试
- c++ - PyBind11:返回对 std::unique_ptr 的 std::vector 的 const 引用
- react-native - React-Native FilesyStem 下载
- range - 如何将 context.document.getSelection().getTextRanges(["."], true) 转换为范围
- python - 如何在黑色背景中去除深色文本
- c++ - 如何解决 C++ 中的 sonarqube 静态代码分析错误“显式定义缺少的复制构造函数,移动构造函数..”
- angular - 我的 NgFor 有什么问题?仅支持绑定到 Iterables,例如 Arrays
- python - SQLAlchemy 过滤器将 datetime.time 转换为 datetime.datetime
- javascript - 如何在 Google 跟踪代码管理器限制中的表单验证后执行代码?
- python - 使用 selenium 驱动程序在 Odoo 中启动 selenium