def test_null_argument(self): authors = Author.objects.annotate( nullif=NullIf("name", Value(None)) ).values_list("nullif") self.assertSequenceEqual(authors, [("John Smith",), ("Rhonda",)])
def _get_queryset(self, cl, form_data): cqs = Checkin.objects.filter( position_id=OuterRef('pk'), list_id=cl.pk).order_by().values('position_id').annotate( m=Max('datetime')).values('m') cqsin = cqs.filter(type=Checkin.TYPE_ENTRY) cqsout = cqs.filter(type=Checkin.TYPE_EXIT) qs = OrderPosition.objects.filter(order__event=self.event, ).annotate( last_checked_in=Subquery(cqsin), last_checked_out=Subquery(cqsout), auto_checked_in=Exists( Checkin.objects.filter( position_id=OuterRef('pk'), list_id=cl.pk, auto_checked_in=True))).prefetch_related( 'answers', 'answers__question', 'addon_to__answers', 'addon_to__answers__question').select_related( 'order', 'item', 'variation', 'addon_to', 'order__invoice_address', 'voucher', 'seat') if not cl.all_products: qs = qs.filter( item__in=cl.limit_products.values_list('id', flat=True)) if cl.subevent: qs = qs.filter(subevent=cl.subevent) if form_data.get('date_from'): dt = make_aware( datetime.combine( dateutil.parser.parse(form_data['date_from']).date(), time(hour=0, minute=0, second=0)), self.event.timezone) qs = qs.filter(subevent__date_from__gte=dt) if form_data.get('date_to'): dt = make_aware( datetime.combine( dateutil.parser.parse(form_data['date_to']).date() + timedelta(days=1), time(hour=0, minute=0, second=0)), self.event.timezone) qs = qs.filter(subevent__date_from__lt=dt) o = () if self.event.has_subevents and not cl.subevent: o = ('subevent__date_from', 'subevent__name') sort = form_data.get('sort') or 'name' if sort == 'name': qs = qs.order_by( *o, Coalesce( NullIf('attendee_name_cached', Value('')), NullIf('addon_to__attendee_name_cached', Value('')), NullIf('order__invoice_address__name_cached', Value('')), 'order__code')) elif sort == 'code': qs = qs.order_by(*o, 'order__code') elif sort.startswith('name:'): part = sort[5:] qs = qs.annotate(resolved_name=Case( When(attendee_name_cached__ne='', then='attendee_name_parts'), When(addon_to__attendee_name_cached__isnull=False, addon_to__attendee_name_cached__ne='', then='addon_to__attendee_name_parts'), default='order__invoice_address__name_parts', )).annotate(resolved_name_part=JSONExtract( 'resolved_name', part)).order_by(*o, 'resolved_name_part') if form_data.get('attention_only'): qs = qs.filter( Q(item__checkin_attention=True) | Q(order__checkin_attention=True)) if not cl.include_pending: qs = qs.filter(order__status=Order.STATUS_PAID) else: qs = qs.filter(order__status__in=(Order.STATUS_PAID, Order.STATUS_PENDING)) return qs
def get_field(self): return (Cast('debateteam__teamscore__votes_given', FloatField()) / NullIf('debateteam__teamscore__votes_possible', 0, output_field=FloatField()) * self.adjs_per_debate)
def test_null_literal(self): msg = 'Oracle does not allow Value(None) for expression1.' with self.assertRaisesMessage(ValueError, msg): list( Author.objects.annotate( nullif=NullIf(Value(None), 'name')).values_list('nullif'))
def test_too_few_args(self): msg = "'NullIf' takes exactly 2 arguments (1 given)" with self.assertRaisesMessage(TypeError, msg): NullIf('name')
def test_null_argument(self): authors = Author.objects.annotate( nullif=NullIf('name', Value(None))).values_list('nullif') self.assertSequenceEqual(authors, [('John Smith', ), ('Rhonda', )])