django-models - 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 {}



这是原始的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)



时间: 原作者:

尝试将. distinct() 添加到查询的末尾。 如文档. all() 所述,不会消除重复行,但. distinct() 将。

以下是相关的文档:https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.distinct

原作者:
...