列表在查询中不起作用
谁能告诉我为什么这个查询只有在我硬编码 phone_type
时才会处理
#model.py
class ClientPhone(models.Model):
client = models.ForeignKey(Client, editable=False)
...
phone_type = models.CharField(max_length=5, choices=PHONE_CHOICES)
number = models.CharField(max_length=24)
# models.py
PHONE_CHOICES = (
('home', 'Home'),
('home2', 'Home 2'),
('mobi', 'Mobile'),
('mobi2', 'Mobile 2'),
('work', 'Work'),
('work2', 'Work 2'),
)
# views.py
phones = [ClientPhone.objects.filter(client=i_clientKEY).filter(phone_type=k).latest('created') for k, v in PHONE_CHOICES]
# shell
>>> from client.models import ClientPhone
>>> phones = [ClientPhone.objects.filter(client=1).filter(phone_type=k).latest('created') for k, v in PHONE_CHOICES]
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 395, in latest
return obj.get()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 341, in get
% self.model._meta.object_name)
DoesNotExist: ClientPhone matching query does not exist.
>>> phones = [ClientPhone.objects.filter(client=1).filter(phone_type='home').latest('created') for k, v in PHONE_CHOICES]
>>> phones
[<ClientPhone: ClientPhone object>, <ClientPhone: ClientPhone object>, <ClientPhone: ClientPhone object>, <ClientPhone: ClientPhone object>, <ClientPhone: ClientPhone object>, <ClientPhone: ClientPhone object>]
Can anyone tell me why this query will only process if I hard code phone_type
#model.py
class ClientPhone(models.Model):
client = models.ForeignKey(Client, editable=False)
...
phone_type = models.CharField(max_length=5, choices=PHONE_CHOICES)
number = models.CharField(max_length=24)
# models.py
PHONE_CHOICES = (
('home', 'Home'),
('home2', 'Home 2'),
('mobi', 'Mobile'),
('mobi2', 'Mobile 2'),
('work', 'Work'),
('work2', 'Work 2'),
)
# views.py
phones = [ClientPhone.objects.filter(client=i_clientKEY).filter(phone_type=k).latest('created') for k, v in PHONE_CHOICES]
# shell
>>> from client.models import ClientPhone
>>> phones = [ClientPhone.objects.filter(client=1).filter(phone_type=k).latest('created') for k, v in PHONE_CHOICES]
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 395, in latest
return obj.get()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 341, in get
% self.model._meta.object_name)
DoesNotExist: ClientPhone matching query does not exist.
>>> phones = [ClientPhone.objects.filter(client=1).filter(phone_type='home').latest('created') for k, v in PHONE_CHOICES]
>>> phones
[<ClientPhone: ClientPhone object>, <ClientPhone: ClientPhone object>, <ClientPhone: ClientPhone object>, <ClientPhone: ClientPhone object>, <ClientPhone: ClientPhone object>, <ClientPhone: ClientPhone object>]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果未找到匹配项,
latest()
会引发异常,因此除非每个客户端都具有每个phone_type
,否则当客户端没有其中之一时,您将收到异常home
、home2
、mobi
等选择。在第二个示例中,您只需要查找
home
,它显然具有latest()
的有效结果。一种方法:
如果您只对模型中的某些特定值感兴趣,则另一种更有效的方法:
latest()
raises an exception if no matches are found, so unless each client has every singlephone_type
, you will get an exception when a client doesn't have one of thehome
,home2
,mobi
, etc choices.In the second example, you are only ever looking for
home
, which apparently has a valid result forlatest()
.One method:
Another more efficient method if you're interested in only some specific values from the model:
您可以使用
get_or_create()
方法而不是latest()
来确保所有手机类型都在数据库中。You can use
get_or_create()
method instead oflatest()
to ensure that all phone types are in the database.