0

I'm coding a django project that use django-filter:

filter.py:

import django_filters
from myApp.models import MyRunStats

class MyRunStatsFilter(django_filters.FilterSet):
    def gen_choice(filed):
       return tuple((l, l) for l in list(AegisRunStats.objects.values_list(filed, flat=True).distinct()))
    
    name_procss=django_filters.ChoiceFilter(label='Process',choices=gen_choice('name_procss'))

    class Meta:
        model = MyRunStats
        fields = ['name_procss']

For some reason I need to add a init function:

class MyRunStatsFilter(django_filters.FilterSet):
    def __init__(self, *args, **kwargs):

        super(MyRunStatsFilter, self).__init__(*args, **kwargs)

        def gen_choice(filed):
            return tuple((l, l) for l in list(AegisRunStats.objects.values_list(filed, flat=True).distinct()))
     
        name_procss=django_filters.ChoiceFilter(label='Process',choices=gen_choice('name_procss'))


    class Meta:
        model = MyRunStats

        fields = ['name_procss']

But after modify the code ,the gen_choice function is not called .

My question is how to call gen_choice() functioin again after I put it under def __init__(self, *args, **kwargs):

William
  • 3,724
  • 9
  • 43
  • 76

0 Answers0