我有以下内容:
PHONE_CHOICES = (
('home', 'Home'),
('home2', 'Home 2'),
('mobi', 'Mobile'),
('mobi2', 'Mobile 2'),
('work', 'Work'),
('work2', 'Work 2'),
)
phones = []
for k, v in PHONE_CHOICES:
try:
phones.append(ClientPhone.objects.filter(client=clientKEY, phone_type=k).latest('id'))
except ClientPhone.DoesNotExist:
pass
我在ClientPhone
中也有一个名为deleted
的属性。如果phones
为空,我只希望将最新电话附加到deleted
列表。
答案 0 :(得分:2)
告诉你的问题有点困难,但我想你想要这样的事情:
phones = []
for k, v in PHONE_CHOICES:
try:
current = ClientPhone.objects.filter(client=clientKEY, phone_type=k)
if current.deleted is None:
phones.append(current.latest('id'))
except ClientPhone.DoesNotExist:
pass