简体   繁体   中英

How to create a filter in Django with two different status

I want to create a filter in Django admin which would return records with two different statuses by single "filter" defined as one of:

def lookups(self, request, model_admin):
    return (
        ('1', 'class 1'),
        ('2', 'class 2'),
        ('3', 'class 3'),
        ('4', 'class 3')
    )

I'm using an API that returns to me all statuses with two extra statuses which I don't need.

def queryset(self, request, queryset):
    if self.value() == 'all':
        return queryset.filter()
    else:
        return queryset.filter(client__status=self.value())

In my filter list I want to have just one logical item clients with status x which would give me all clients with status M and status N.

def queryset(self, request, queryset):
    if self.value() == 'all':
        return queryset.all()
    elif self.value() == 'clients with status x':
        return queryset.filter(client__status__in=['M', 'N', ])
    else:
        return queryset.filter(client__status=self.value())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM