覆盖导入的类变量 - django / python

问题描述 投票:0回答:1

我需要覆盖变量(或传递动态数据)到导入的类。

filters.朋友

import django_filters
from .models import Gate, Tram, OperationArea, Bogie
from distutils.util import strtobool
from django import forms


class GateFilter(django_filters.FilterSet):

    # Prepare dynamic lists with choices
    tram_list = [(id, number) for id, number in Tram.objects.all().values_list('id', 'number')]
    bogie_list = [(id, number) for id, number in Bogie.objects.all().values_list('id', 'number')]
    area_list = [(id, area) for id, area in OperationArea.objects.all().values_list('id', 'area')]
    # Generate fields
    tram = django_filters.MultipleChoiceFilter(choices=tram_list, label=u'Tramwaj')
    car = django_filters.MultipleChoiceFilter(choices=Gate.CAR_SYMBOLS, label=u'Człon')
    bogie = django_filters.MultipleChoiceFilter(choices=bogie_list, label=u'Wózek')
    bogie_type = django_filters.MultipleChoiceFilter(choices=Gate.BOGIE_TYPES, label=u'Typ wózka')
    area = django_filters.MultipleChoiceFilter(choices=area_list, label=u'Obszar')
    operation_no = django_filters.CharFilter(label=u'Numer operacji', widget=forms.TextInput(attrs={'size': '16px'}))
    status = django_filters.MultipleChoiceFilter(choices=Gate.GATE_STATUSES, label=u'Status')
    rating = django_filters.MultipleChoiceFilter(choices=Gate.GATE_GRADES, label=u'Ocena')

    class Meta:
        pass

views.朋友

from .filters import GateFilter

class GateListView(generic.ListView):

    queryset = None
    gate_type = None
    template_name = 'qapp/gate/list.html'
    context_object_name = 'gate_list'
    paginate_by = 20

    def get_queryset(self):
        # Type is stored in database as big-letter word, so 'bjc' != 'BJC'.
        if self.gate_type.upper() == 'BJW':
            ordering = ['bogie', 'bogie_type']
        else:
            ordering = ['tram', 'car']
        queryset = Gate.objects.filter(type=self.gate_type.upper()).order_by(*ordering)
        self.gate_list = GateFilter(self.request.GET, queryset=queryset)
        return self.gate_list.qs.distinct()

    def get_context_data(self, **kwargs):
        context = super(GateListView, self).get_context_data(**kwargs)
        # Return Gate.type to template.
        context['gate_type'] = self.gate_type
        # Return object (for generating form) to template.
        context['gate_list_filter'] = self.gate_list
        return context

如您所见,在filters.py中,变量tram_list,bogie_list和area_list的数据是动态的(从数据库中获取)。

但在将此类导入views.py期间,此数据将变为静态。

我试图覆盖这个值:

  • 在类GateFilter中使用@classmethod decorator,并在设置self.gate_list对象之前调用它,
  • 在views.py中使用GateFilter.tram_list(和其余的)表示法,

没运气。

由于导入类型(来自.filters导入GateFilter),我无法使用reload()函数。

目前在filters.py中的更新列表我需要重新运行整个应用程序。这对我的应用程序的业务逻辑来说是不可接受的。

python django class django-filter
1个回答
1
投票

这是错误的做法。相反,您应该使用知道查询集的过滤器,并在需要时对其进行评估:ModelChoiceFilter和ModelMultipleChoiceFilter。

class GateFilter(django_filters.FilterSet):
    team = django_filters.ModelMultipleChoiceFilter(queryset=Tram.objects.all())
© www.soinside.com 2019 - 2024. All rights reserved.