(domain)
return query
The base query includes a 'date_last_activity' field. This field
    is calculated as:
      Max(
        event.date,
        xform_session.modified_time,  # if it exists
        Max(sms.date_modified),  # max for the current event
        Max(email.date_modified)  # max for the current event
      )
The base query includes a 'date_last_activity' field. This field
    is calculated as:
      Max(
        event.date,
        xform_session.modified_time,  # if it exists
        Max(sms.date_modified),  # max for the current event
        Max(email.date_modified)  # max for the current event
      )
The base query includes a date_last_activity field . This field is calculated as : Max ( event . date xform_session . modified_time # if it exists Max ( sms . date_modified ) # max for the current event Max ( email . date_modified ) # max for the current event )
def _get_base_query(domain):
    """The base query includes a 'date_last_activity' field. This field
    is calculated as:
      Max(
        event.date,
        xform_session.modified_time,  # if it exists
        Max(sms.date_modified),  # max for the current event
        Max(email.date_modified)  # max for the current event
      )
  """
    query = MessagingSubEvent.objects.select_related("parent").filter(parent__domain=domain)
    newest_sms = (
        SMS.objects.filter(messaging_subevent=OuterRef('pk'))
        .order_by('-date_modified')
        .values('date_modified')[:1]
    )
    newest_email = (
       Email.objects.filter(messaging_subevent=OuterRef('pk'))
       .order_by('-date_modified')
       .values('date_modified')[:1]
    )
    query = query.annotate(date_last_activity=Greatest(
        'date', 'xforms_session__modified_time', Subquery(newest_sms), Subquery(newest_email)
    ))
    return query
def _get_base_query ( domain ) : query = MessagingSubEvent . objects . select_related ( "parent" ) . filter ( parent__domain = domain ) newest_sms = ( SMS . objects . filter ( messaging_subevent = OuterRef ( 'pk' ) ) . order_by ( '-date_modified' ) . values ( 'date_modified' ) [ : 1 ] ) newest_email = ( Email . objects . filter ( messaging_subevent = OuterRef ( 'pk' ) ) . order_by ( '-date_modified' ) . values ( 'date_modified' ) [ : 1 ] ) query = query . annotate ( date_last_activity = Greatest ( 'date' , 'xforms_session__modified_time' , Subquery ( newest_sms ) , Subquery ( newest_email ) ) ) return query