Search code examples
pythondjangodjango-tables2

Django Tables 2 Filter field, but use the display name?


I've got this model:

DRAFT = 'draft'
IN_PROGRESS = 'in_progress'
FINISHED = 'finished'

STATUS_CHOICES = (
    (DRAFT, 'Entwurf'),
    (IN_PROGRESS, 'In Arbeit'),
    (FINISHED, 'Erledigt'),
)


class Invoice(TimeStampModel):
    status = models.CharField('Status', choices=STATUS_CHOICES, max_length=50)

and this filter I've build with the django tables 2 plugin:

class InvoiceFilter(django_filters.FilterSet):

        class Meta:
            model = Invoice
            fields = {
                'name': ['icontains'],
                'status': ['icontains'],
            }

My filter gets rendered without a problem on my page and I can use it. The problem right now is though if I try to search for a status using the german word, like for example Entwurf the filter always returns me no item. BUT if I filter for draft I get all the items which have this status. So my question would be: how can I tell my filter to look for the display name of my status field?

Or even better: would it be possible to just make a select out of the status field instead of a normal search?

Thanks for any answers!


Solution

  • Alright. I should have looked a little closer into the docs of django tables 2. I found my solution:

    class InvoiceFilter(django_filters.FilterSet):
            status = django_filters.TypedChocieFilter(choices=STATUS_CHOICES)
    
            class Meta:
                model = Invoice
                fields = {
                    'name': ['icontains'],
                }