这个问题是很久以前发布的,但是我遇到了类似的问题,在这里很难找到答案。接受的答案仅允许您按first_name和last_name查找完全匹配。第二个答案要好一些,但仍然很糟糕,因为您尽可能多地访问数据库。这是我的解决方案,将first_name和last_name注释在一起并在此字段中搜索:
from django.db.models import Value as Vfrom django.db.models.functions import Concat users = User.objects.annotate(full_name=Concat('first_name', V(' '), 'last_name')). filter(full_name__icontains=query)例如,如果此人的名字是John Smith,则可以通过键入john smith,john,smith,hn smi等找到他。它仅命中数据库。我认为这将是您在公开帖子中想要的确切SQL。



