- Django模型
- 序列化器
- 试图filter_queryset集合完成重新拼接
- 使用queryset = chain(queryset1, queryset2)完成有序拼接
- 路由
- 实现
class Book(models.Model): """书名""" title = models.CharField(max_length=255, verbose_name='书名') num = models.IntegerField(verbose_name='数量') name = models.ForeignKey(User,max_length=255, on_delete=models.CASCADE, blank=True,verbose_name='logo的url') price = models.CharField(max_length=255,verbose_name='价格') create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间') class meta: verbose_name_plural = '图书' db_table = 'books' ordering = ('id',) def __str__(self): return str(self.name)序列化器
class BookSerializers(serializers.ModelSerializer): """图书表序列化""" username = serializers.SerializerMethodField() class meta: model = Book fields = '__all__' def get_username(self,obj): return obj.name.username试图filter_queryset集合完成重新拼接 使用queryset = chain(queryset1, queryset2)完成有序拼接
from itertools import chain from django.db.models import QuerySet from django.shortcuts import render # Create your views here. from rest_framework.filters import OrderingFilter from rest_framework.viewsets import ModelViewSet from app1.models import Book from app1.seleialize import BookSerializers class BooksModelViewSet(ModelViewSet): queryset = Book.objects.all() serializer_class = BookSerializers filter_backends = [OrderingFilter] ordering_fields = ('id', 'num', 'price') def filter_queryset(self, queryset): # 排序 queryset = super().filter_queryset(queryset) # 获取当前用户 user = self.request.user # 获取当前用户记录 queryset1 = queryset.filter(name=user) # 获取除了当前用户的记录 queryset2 = queryset.exclude(name=user) 拼接 queryset = chain(queryset1, queryset2) return queryset路由
http://127.0.0.1:8000/app1/book/?ordering=num,-price
字段num升序,当num相同时,按照price降序排序
HTTP 200 OK Allow: GET, POST, HEAD, OPTIONS Content-Type: application/json Vary: Accept [ { "id": 12, "username": "root", "title": "计算机组成原理", "num": 33, "price": "98", "create_time": "2021-11-15T09:19:39.950932", "name": 2 }, { "id": 9, "username": "root", "title": "概率统计", "num": 33, "price": "45", "create_time": "2021-11-12T09:30:15.763232", "name": 2 }, { "id": 8, "username": "root", "title": "线性代数", "num": 55, "price": "45", "create_time": "2021-11-12T09:30:04.173884", "name": 2 }, { "id": 7, "username": "root", "title": "高等数学", "num": 88, "price": "222", "create_time": "2021-11-12T09:29:46.058903", "name": 2 }, { "id": 3, "username": "admin", "title": "C", "num": 16, "price": "47", "create_time": "2021-11-12T09:28:28.952346", "name": 1 }, { "id": 2, "username": "admin", "title": "java", "num": 24, "price": "58", "create_time": "2021-11-12T09:28:09", "name": 1 }, { "id": 1, "username": "admin", "title": "Python", "num": 32, "price": "59", "create_time": "2021-11-12T09:27:36.402378", "name": 1 }, { "id": 4, "username": "admin", "title": "javascript", "num": 33, "price": "67", "create_time": "2021-11-12T09:28:42.322505", "name": 1 }, { "id": 5, "username": "lidong", "title": "大学英语1", "num": 34, "price": "45", "create_time": "2021-11-12T09:29:07.834414", "name": 3 }, { "id": 6, "username": "lidong", "title": "大学英语2", "num": 54, "price": "65", "create_time": "2021-11-12T09:29:21.960366", "name": 3 }, { "id": 10, "username": "admin", "title": "C语言——谭浩强版", "num": 57, "price": "59", "create_time": "2021-11-12T09:30:37.685550", "name": 1 }, { "id": 11, "username": "admin", "title": "Java——黑马科技", "num": 68, "price": "90", "create_time": "2021-11-12T09:31:06.174506", "name": 1 } ]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)