这很奇怪,这个查询
originator = User.objects.get(Q(emailaddress__email__exact=fromfield) | Q(phonenumber__exact=fromfield))
返回两个对象,当我单独查询时,我只得到一个对象。我不明白为什么会这样:(
>>> originator = User.objects.get(emailaddress__email__exact=fromfield)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/site/gruppe/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/usr/site/gruppe/lib/python2.7/site-packages/django/db/models/query.py", line 401, in get
(self.model._meta.object_name, kwargs))
DoesNotExist: GruppUser matching query does not exist. Lookup parameters were {'emailaddress__email__exact': 9135261967L}
>>> originator = User.objects.get(phonenumber__exact=fromfield)
>>> originator
<GruppUser: jorge>
>>> originator = User.objects.get(Q(emailaddress__email__exact=fromfield) | Q(phonenumber__exact=fromfield))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/site/gruppe/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/usr/site/gruppe/lib/python2.7/site-packages/django/db/models/query.py", line 405, in get
(self.model._meta.object_name, num, kwargs))
MultipleObjectsReturned: get() returned more than one GruppUser -- it returned 2! Lookup parameters were {}
这是raw中的queryset sql:
(u'SELECT "gruppu_gruppuser"."password", "gruppu_gruppuser"."last_login", "gruppu_gruppuser"."is_superuser", "gruppu_gruppuser"."id", "gruppu_gruppuser"."username", "gruppu_gruppuser"."first_name", "gruppu_gruppuser"."last_name", "gruppu_gruppuser"."email", "gruppu_gruppuser"."is_staff", "gruppu_gruppuser"."is_active", "gruppu_gruppuser"."date_joined", "gruppu_gruppuser"."phonenumber", "gruppu_gruppuser"."mmsemail", "gruppu_gruppuser"."smsemail", "gruppu_gruppuser"."verified", "gruppu_gruppuser"."realname", "gruppu_gruppuser"."location", "gruppu_gruppuser"."website", "gruppu_gruppuser"."bio", "gruppu_gruppuser"."synctoFB" FROM "gruppu_gruppuser" LEFT OUTER JOIN "account_emailaddress" ON ("gruppu_gruppuser"."id" = "account_emailaddress"."user_id") WHERE ("account_emailaddress"."email" = %s OR "gruppu_gruppuser"."phonenumber" = %s )', (u'9135261967', u'9135261967'))
它选择了同一个用户:
>>> queryset.all()
[<GruppUser: jorge>, <GruppUser: jorge>]
SQL真的选择了两个,这对我来说是黑魔法:(:
password | last_login | is_superuser | id | username | first_name | last_name | email | is_staff | is_active | date_joined | phonenumber | mmsemail | smsemail | verified | realname | location | website | bio | synctoFB
-------------------------------------------------------------------------------+-------------------------------+--------------+----+----------+------------+-----------+------------------+----------+-----------+------------------------+-------------+--------------------------+------------------------------------+----------+----------+----------+---------+-----+----------
xxxx | 2014-02-09 08:49:06.225694+04 | f | 2 | jorge | Jorge | Sanchez | x@x.c| f | t | 2013-10-08 14:53:16+04 | 9135261967 | 9135261967@xx | 9135261967@xx | t | | Prague | | | t
xxxx| 2014-02-09 08:49:06.225694+04 | f | 2 | jorge | Jorge | Sanchez | x@x.c | f | t | 2013-10-08 14:53:16+04 | 9135261967 | 9135261967@xx | 9135261967@xx | t | | Prague | | | t
(2 rows)
答案 0 :(得分:0)
尝试在查询结尾添加.distinct()。如文档中所述.all()不会消除重复的行,但.distinct()会。
以下是相关文件: https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.distinct