简体   繁体   English

覆盖导入的类变量-Django / Python

[英]Override imported class variables - django/python

I need to override variables (or pass dynamic data) to imported class. 我需要将变量(或传递动态数据)覆盖到导入的类。

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

As you can see, in the filters.py , the data for variables tram_list, bogie_list and area_list are dynamic (fetched from database). 正如你所看到的,在filters.py,变量tram_list,bogie_listarea_list的数据是动态的(从数据库中提取)。

But during importing this class to views.py, this data becomes static. 但是在将此类导入views.py期间,此数据将变为静态。

I tried to override this values: 我试图覆盖此值:

  • using @classmethod decorator in class GateFilter , and calling it before setting self.gate_list object, GateFilter类中使用@classmethod装饰器,并在设置self.gate_list对象之前对其进行调用,
  • in views.py using GateFilter.tram_list (and the rest) notation, 在使用GateFilter.tram_list (和其余)符号的views.py中

No luck. 没运气。

I can't use reload() function, due to import type ( from .filters import GateFilter ). 由于导入类型( 来自.filters import GateFilter ),我无法使用reload()函数。

Currently for update lists in filters.py I need to rerun whole app. 目前有关filters.py中的更新列表,我需要重新运行整个应用程序。 This is unacceptable for business logic of my app. 这对于我的应用程序的业务逻辑是无法接受的。

This is the wrong approach. 这是错误的方法。 Rather, you should be using the filters that are aware of querysets and that evaluate them when required: ModelChoiceFilter and ModelMultipleChoiceFilter. 相反,您应该使用知道查询集并在需要时对其进行评估的筛选器: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