簡體   English   中英

覆蓋導入的類變量-Django / Python

[英]Override imported class variables - django/python

我需要將變量(或傳遞動態數據)覆蓋到導入的類。

filters.py

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.py

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_listarea_list的數據是動態的(從數據庫中提取)。

但是在將此類導入views.py期間,此數據將變為靜態。

我試圖覆蓋此值:

  • GateFilter類中使用@classmethod裝飾器,並在設置self.gate_list對象之前對其進行調用,
  • 在使用GateFilter.tram_list (和其余)符號的views.py中

沒運氣。

由於導入類型( 來自.filters import GateFilter ),我無法使用reload()函數。

目前有關filters.py中的更新列表,我需要重新運行整個應用程序。 這對於我的應用程序的業務邏輯是無法接受的。

這是錯誤的方法。 相反,您應該使用知道查詢集並在需要時對其進行評估的篩選器:ModelChoiceFilter和ModelMultipleChoiceFilter。

class GateFilter(django_filters.FilterSet):
    team = django_filters.ModelMultipleChoiceFilter(queryset=Tram.objects.all())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM