class Book(models.Model): """ 列名 """ class Meta: db_table = 'book' name = models.CharField(verbose_name="名字", max_length=32) sort = models.IntegerField(verbose_name="排序", default=None, null=True, blank=True) describe = models.CharField(verbose_name="描述", max_length=128, null=True)
t_name = get_json_values('t_name', body) t_sort = get_json_values('t_sort', body) t_id = get_json_values('t_id', body) case = Case( When(Q(Q(name__icontains=t_name) & Q(sort=t_sort) & Q(id=t_id)), then=1), When(Q(Q(name__icontains=t_name) & Q(id=t_id)), then=2), When(Q(Q(name__icontains=t_name) & Q(sort=t_sort)), then=3), When(Q(Q(sort=t_sort) & Q(id=t_id)), then=4), default=5, output_field=CharField() ) books= models.Book.objects.filter( Q(Q(name__icontains=t_name) & Q(sort=t_sort) & Q(id=t_id))| Q(Q(name__icontains=t_name) & Q(id=t_id)) | Q(Q(name__icontains=t_name) & Q(sort=t_sort)) | Q(Q(sort=t_sort) & Q(id=t_id))| Q(name__icontains=t_name)|Q(sort=t_sort)|Q(id=t_id) ).annotate(pos=case).order_by('pos') print(books)
标签:sort,__,name,models,查询,django,icontains,排序,id From: https://www.cnblogs.com/moon3496694/p/17120569.html