Django query filter, return object if attr delete is false (i.e if it hasn't been deleted)
I have the following:
PHONE_CHOICES = (
('home', 'Home'),
('home2', 'Home 2'),
('mobi', 'Mobile'),
('mobi2', 'Mobile 2'),
('work', 'Work'),
开发者_StackOverflow ('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
I also have an attribute in ClientPhone
called deleted
. I only want it to append the most resent phone to the phones
list if deleted
is null.
It is somewhat difficult to tell what you are asking, but I think you want something like this:
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
精彩评论