0
這是很奇怪的,這個查詢Django的模型查詢行爲strangly
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 {}
而且這是在原始的查詢集的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 | [email protected]| f | t | 2013-10-08 14:53:16+04 | 9135261967 | [email protected] | [email protected] | t | | Prague | | | t
xxxx| 2014-02-09 08:49:06.225694+04 | f | 2 | jorge | Jorge | Sanchez | [email protected] | f | t | 2013-10-08 14:53:16+04 | 9135261967 | [email protected] | [email protected] | t | | Prague | | | t
(2 rows)
請清潔香港查詢的原始SQL。您打印查詢集的查詢屬性。 SELECT >>>「myapp_mymodel」。「id」,... FROM「myapp_mymodel」,可能是你得到你的錯誤。 – Aks