How to filter by custom field in django admin, the value of which is calculated

61 Views Asked by At

I have a task to calculate the status of the celery task and display it in the admin panel, as well as add filtering by statuses, all my errors lead to an error that says that the database does not have a status field.

I also wrote a custom filter

class TaskStatusFilter(admin.SimpleListFilter):
    title = 'status'
    parameter_name = 'status' 

    def lookups(self, request, model_admin):  
        return (
            ('success', 'SUCCESS'),
            ('failure', 'FAILURE'),
            ('pending', 'PENDING'),
            ('started', 'STARTED'),
            ('retry', 'RETRY'),
            ('revoked', 'REVOKED'),
            ("timeout", "TIMEOUT"),
            ('started_on_retry', 'STARTED_ON_RETRY')
        )

    def queryset(self, request, queryset):  
        if self.value() == 'SUCCESS':
            queryset = queryset.filter(status='SUCCESS')
        elif self.value() == 'FAILURE':
            queryset = queryset.filter(status='FAILURE')
        elif self.value() == 'PENDING':
            queryset = queryset.filter(status='PENDING')
        elif self.value() == 'STARTED':
            queryset = queryset.filter(status='STARTED')
        elif self.value() == 'RETRY':
            queryset = queryset.filter(status='RETRY')
        elif self.value() == 'REVOKED':
            queryset = queryset.filter(status='REVOKED')
        elif self.value() == 'TIMEOUT':
            queryset = queryset.filter(status='TIMEOUT')
        elif self.value() == 'STARTED_ON_RETRY':
            queryset = queryset.filter(status='STARTED_ON_RETRY')
        return queryset

But queryset does not contain status

Here is the function of getting the status

def status(self, obj):
    status = get_task_status(obj.task_uuid)
    return status
0

There are 0 best solutions below