开发者

Django query filter, return object if attr delete is false (i.e if it hasn't been deleted)

开发者 https://www.devze.com 2023-02-16 00:26 出处:网络
I have the following: PHONE_CHOICES = ( (\'home\', \'Home\'), (\'home2\', \'Home 2\'), (\'mobi\', \'Mobile\'),

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
0

精彩评论

暂无评论...
验证码 换一张
取 消