django - How to include "None" in lte/gte comparisons? -
i've got complex filtering mechanism...
d = copy(request.get) d.setdefault('sort_by', 'created') d.setdefault('sort_dir', 'desc') form = filtershipmentform(d) filter = { 'status': shipmentstatuses.active } exclude = {} if not request.user.is_staff: filter['user__is_staff'] = false if request.user.is_authenticated(): exclude['user__blocked_by__blocked'] = request.user if form.is_valid(): d = form.cleaned_data if d.get('pickup_city'): filter['pickup_address__city__icontains'] = d['pickup_city'] if d.get('dropoff_city'): filter['dropoff_address__city__icontains'] = d['dropoff_city'] if d.get('pickup_province'): filter['pickup_address__province__exact'] = d['pickup_province'] if d.get('dropoff_province'): filter['dropoff_address__province__exact'] = d['dropoff_province'] if d.get('pickup_country'): filter['pickup_address__country__exact'] = d['pickup_country'] if d.get('dropoff_country'): filter['dropoff_address__country__exact'] = d['dropoff_country'] if d.get('min_price'): filter['target_price__gte'] = d['min_price'] if d.get('max_price'): filter['target_price__lte'] = d['max_price'] if d.get('min_distance'): filter['distance__gte'] = d['min_distance'] * 1000 if d.get('max_distance'): filter['distance__lte'] = d['max_distance'] * 1000 if d.get('available_on'): # <--- relevant bit here --- filter['pickup_earliest__lte'] = d['available_on'] # want "lte or none" filter['pickup_latest__gte'] = d['available_on'] if d.get('shipper'): filter['user__username__iexact'] = d['shipper'] order = ife(d['sort_dir'] == 'desc', '-') + d['sort_by'] shipments = shipment.objects.filter(**filter).exclude(**exclude).order_by(order) \ .annotate(num_bids=count('bids'), min_bid=min('bids__amount'), max_bid=max('bids__amount'))
and client tells me wants pickup/drop-off dates 'flexible' option. i've updated db allow dates null purpose, "available pickup on" filter won't work expected. should include null/none dates. there easy fix this?
most database engines don't relational comparisons null
values. use <field>__isnull
explicitly check if value null
in database, you'll need use q
objects or
conditions together.
Comments
Post a Comment