django conditional filter OR
if w:
conditions['is_discount_table'] = 'Q( is_discount_table = True )'
conditions['is_discount_banquet'] = 'Q( is_discount_banquet = True )'
pl = Place.objects.filter( **conditions ).order_by( 'name' )
SQL WHERE looks开发者_如何转开发 like
WHERE (
"places_place"."is_discount_banquet" = true AND
"places_place"."is_discount_table" = true
)
How to modify w
condition that SQL WHERE looked like
WHERE (
"places_place"."is_discount_banquet" = true OR
"places_place"."is_discount_table" = true
)
equivalent of
Place.objects.filter( Q( is_discount_table = True ) | Q( is_discount_banquet = True ) )
i found the answer:
conditions_unnamed = list()
conditions_named = dict( )
if type:
conditions_named['types__id__in'] = list_unique( type.split( '-' ) )
if w:
conditions_unnamed.append( Q( is_discount_table = True ) | Q( is_discount_banquet = True ) )
pl = Place.objects.filter( *conditions_unnamed, **conditions_named ).order_by( 'name' )
if somebody knows a better solution, please share.
You seem to have added some additional logic. I think this is what you are trying to express:
pl = Place.objects.all()
if type:
pl = pl.filter(types__id__in=type.split( '-' ))
if w:
pl = pl.filter( Q( is_discount_table = True ) | Q( is_discount_banquet = True ) )
pl = pl.order_by( 'name' )
queres = {Q()}
...
if ...
queres.update({Q(deprecation_date__lte=datetime.date.today())})
if ...
queres.update({Q(deprecation_date=None)})
...
devices = Device.objects.filter(*queres)
精彩评论