本文介绍: 【代码】django mysql in 有序返回。
from django.db.models import *
ordering = f"FIELD(`id`, {','.join([str(_) for _ in ids])})" # 默认就按照算法返回的 id 排序
        p_data_result = PeptidesDataResult.objects.using("polypeptide").filter(id__in=ids).values().extra(select={'ordering': ordering}, order_by=('ordering',))



# 起别名

whens = [When(Q(reference__isnull=False) | ~Q(reference='') | Q(patent__isnull=False) | ~Q(patent=''), then=1)] # 计算是否 有文献
data = PeptidesDataResult.objects.using("polypeptide")
        .annotate(has_refer=Case(*whens, default=0))

参考:Mysql 指定字段数据排序 以及django的实现 – Python技术站

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注