I've had to look for this a few times in the past few days, so I figured I should post it here.
from django.db import DEFAULT_DB_ALIAS Foo.objects.all().query.get_compiler(DEFAULT_DB_ALIAS).as_sql()
or
Foo.objects.all().query.get_compiler('default').as_sql()