Add condition based on Django version for comparing generated SQL

Since the internal SQL generation changed (addressed in 15934637), the tests need
to use SQL relative to the Django version.