diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 9433540d7c..05597173ee 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -16,9 +16,16 @@ from django.db.models.sql.query import Query, get_order_dir from django.db.transaction import TransactionManagementError from django.utils.functional import cached_property from django.utils.hashable import make_hashable +from django.utils.regex_helper import _lazy_re_compile class SQLCompiler: + # Multiline ordering SQL clause may appear from RawSQL. + ordering_parts = _lazy_re_compile( + r'^(.*)\s(?:ASC|DESC).*', + re.MULTILINE | re.DOTALL, + ) + def __init__(self, query, connection, using): self.query = query self.connection = connection @@ -31,8 +38,6 @@ class SQLCompiler: self.select = None self.annotation_col_map = None self.klass_info = None - # Multiline ordering SQL clause may appear from RawSQL. - self.ordering_parts = re.compile(r'^(.*)\s(ASC|DESC)(.*)', re.MULTILINE | re.DOTALL) self._meta_ordering = None def setup_query(self):