注释字段上的django-filter过滤器

时间:2019-06-20 23:17:39

标签: python django django-rest-framework

function p=cycling_parameters

p.M = 195.084; 
p.rho = 21.45;       
p.time = linspace(0, 30, 500);
p.m_V__M_Pt = 1e6/p.M;     

p.Vm_Pt = 9.09;  

p.R0_log = -8.1963; 
p.k_dis_log = -13; 
p.k_rdp_log = -11;

p.R0 = 10^(p.R0_log);
p.k_dis = 10^(p.k_dis_log);
p.k_rdp = 10^(p.k_rdp_log);

%%% geometry %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Finite Volume Method with equidistant elements 
p.r_max = 10.1e-9;                    % [m] maximum radius of PRD

p.n = 301;                          % number of elements of FVM

p.dr = p.r_max/p.n;                 % [m] length of elements, equidistant
p.r = linspace(5e-10,p.r_max,p.n+1)';   % [m] boundaries of elements
p.r_m = p.r(1:p.n)+p.dr/2;          % [m] center of elements

%log normal initial distribution

S = 0.314; 
M = 0.944; 
p.x0 = 1./(S.*sqrt(2.*pi).*p.r_m*1e9).*exp((-(log(p.r_m*1e9)-M).^2)./(2.*S.^2));


p.r_squared = p.r_m.^2; % [m^2] squares of the radius (center of elements)
p.r_cubed   = p.r_m.^3; % [m^3] cubes of the radius (center of elements)
p.MN_0 = trapz(p.r_m, p.r_cubed.*p.x0);   % Eq. 2.11 denominator
p.M_0 = 4/3*pi*p.rho*p.MN_0;  
p.I_V = 1; %ionomer volume fraction in the catalyst layer

我有一个要在带注释的字段上过滤的ViewSet,通常您可以只在django查询集中的注释上过滤,但是上面结合了此序列化器:

class EventViewSet(viewsets.ModelViewSet):
    queryset = Event.objects.all()
    serializer_class = EventSerializer

    def get_queryset(self):
        return super().get_queryset().annotate(
            is_active=ExpressionWrapper(
                Q(start_date__lt=timezone.now()) & Q(end_date__gt=timezone.now()),
                output_field=BooleanField()
            ),
        )

    search_fields = [
        'name',
        'short_desc',
        'desc',
    ]

    filterset_fields = [
        'is_active',
    ]

我还没有深入研究源代码,但是我认为它会为class EventSerializer(serializers.ModelSerializer): is_active = serializers.SerializerMethodField() @staticmethod def get_is_active(obj): return obj.is_active class Meta: model = Event fields = [ 'timestamp', 'id', 'name', 'short_desc', 'desc', 'start_date', 'end_date', 'is_active', ] 中的字段做一个简单的qs.filter,但是我遇到了一个无法解释太多错误的漂亮错误(在至少对我来说):

filterset_fields

0 个答案:

没有答案