[Python] how can I filter on the second element in a tuple of tuples?

Discussão em 'Python' iniciado por Stack, Maio 23, 2025.

  1. Stack

    Stack Membro Participativo

    In my model I have a field:

    country = models.CharField(_('Country'), max_length=2, choices=COUNTRIES)


    Where COUNTRIES is a tuple of tuples like this:

    COUNTRIES = (
    ('AF', _('Afghanistan')),


    ... and so on

    Now I want to filter an instance of that model, by the country name.

    This:

    i = MyModel.objects.filter(country__iexact=query)


    only lets me filter by the country code.

    How can I filter by country name?

    Continue reading...

Compartilhe esta Página