动态传递字段并查找 Q 对象中的类型和值

dynamic passing fields and look up types and values in Q objects

这是在用户提供的范围内过滤比赛开始日期和结束日期,下面是适用于相同但我想提供字段名称的代码(此处:deadlines__start_date)和 look_up 类型(此处:__lte、__gte)。以及用户提供的范围字段 self.start_date 和 self.end_date.

return qs.filter((Q(deadlines__start_date__gte=self.start_date) & Q(deadlines__end_date__lte=self.end_date))|
                (Q(deadlines__start_date__lte=self.start_date) & Q(deadlines__end_date__lte=self.end_date) & Q(deadlines__end_date__gte=self.start_date)) |
                (Q(deadlines__start_date__lte=self.start_date) & Q(deadlines__end_date__gte=self.end_date)) |
                (Q(deadlines__start_date__gte=self.start_date) & Q(deadlines__end_date__gte=self.end_date) & Q(deadlines__start_date__lte=self.end_date))
            ) 


this was working fine but now I want to send field names and look_up types and values to be compared dynamically, but not finding any solution. ```


I tried doing it like below:

        kwargs0 = {str('%s__gte' % (start_field)) : str('%s' % self.start_date)}                      
        kwargs1 = {str('%s__lte' % (end_field)) : str('%s' % self.end_date)}        
        kwargs2 = {str('%s__lte' % (start_field)) : str('%s' % self.start_date)}     
        kwargs3 = {str('%s__lte' % (end_field)) : str('%s' % self.end_date)}   
        kwargs4 = {str('%s__gte' % (end_field)) : str('%s' % self.start_date)}  
        kwargs5 = {str('%s__lte' % (start_field)) : str('%s' % self.start_date)}  
        kwargs6 = {str('%s__gte' % (end_field)) : str('%s' % self.end_date)}  
        kwargs7 = {str('%s__gte' % (start_field)) : str('%s' % self.start_date)}  
        kwargs8 = {str('%s__gte' % (end_field)) : str('%s' % self.end_date)}  
        kwargs9 = {str('%s__lte' % (start_field)) : str('%s' % self.end_date)}
        q_object= Q()
        q_object.add(Q(**kwargs0), Q.AND)
        q_object.add(Q(**kwargs1), Q.OR)
        q_object.add(Q(**kwargs2), Q.AND)
        q_object.add(Q(**kwargs3), Q.AND)
        q_object.add(Q(**kwargs4), Q.OR)
        q_object.add(Q(**kwargs5), Q.AND)
        q_object.add(Q(**kwargs6), Q.OR)
        q_object.add(Q(**kwargs7), Q.AND)
        q_object.add(Q(**kwargs8), Q.AND)
        q_object.add(Q(**kwargs9), Q.AND) 
        return qs.filter(q_object)

上下文:我寻找有关如何动态添加字段的解决方案,并已走到这一步。

I beleive the errors are in how I am giving Q.AND and Q.OR arguments and that is creating wrong query, how to do this?
but am getting empty query set as output.
HELP!!!
return qs.filter(                                                                                   
(Q(**{"%s__gte" % self.start_date_field: self.start_date_value }) & Q(**{"%s__lte" % self.end_date_field: self.end_date_value })) |
(Q(**{"%s__lte" % self.start_date_field: self.start_date_value }) & Q(**{"%s__lte" % self.end_date_field: self.end_date_value }) & Q(**{"%s__gte" % self.end_date_field: self.start_date_value })) |
(Q(**{"%s__lte" % self.start_date_field: self.start_date_value }) & Q(**{"%s__gte" % self.end_date_field: self.end_date_value })) |
(Q(**{"%s__gte" % self.start_date_field: self.start_date_value }) & Q(**{"%s__gte" % self.end_date_field: self.end_date_value }) & Q(**{"%s__lte" % self.start_date_field: self.end_date_value }))
            )

像这样格式化 Q 对象有效。