pythondjangoormqobjectdjango-q

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


this is to filter a competition start date and end date in the range provided by the user, below is the code that is working for the same but I want to supply field names (here:deadlines__start_date) and look_up types (here: __lte, __gte). and the user supplied range fields self.start_date and 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)

context: I looked for solutions on how to dynamically add fields and have come this far.

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!!!

Solution

  • 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 }))
                )
    

    formatting the Q object like this worked.