【发布时间】:2014-03-22 11:26:24
【问题描述】:
我有一个 Django 模型
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
现在我想搜索用户。问题是当我这样做时
Q(first_name__icontains=search_string) | Q(last_name__icontains=search_string)
它只分别搜索 first_name 和 last_name。但是,如果有人在搜索栏中输入全名,则不会给出任何结果,因为全名(first_name+last_name)不包含在 first_name 或 last_name 中。 我不想更改我的模型,但在搜索时结合字段(first_name +last_name)动态查询,有什么办法吗?
【问题讨论】:
-
您可以
split搜索字符串并在各自的字段中搜索2个字符串。
标签: django python-2.7 django-q