Skip to content

Commit

Permalink
Update compiler.py (#9)
Browse files Browse the repository at this point in the history
Add django version checking to back compat code
  • Loading branch information
rhomboss authored Apr 24, 2024
1 parent 6661b5e commit 2fcbc05
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions tree_queries/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db.models.functions import RowNumber
from django.db.models.sql.compiler import SQLCompiler
from django.db.models.sql.query import Query
import django


SEPARATOR = "\x1f"
Expand Down Expand Up @@ -166,17 +167,17 @@ def get_rank_table(self):

# Convert strings to expressions. This is to maintain backwards compatibility
# with Django versions < 4.1
base_order = []
for field in order_fields:
if isinstance(field, Expression):
base_order.append(field)
elif isinstance(field, str):
if field[0] == "-":
base_order.append(F(field[1:]).desc())
else:
base_order.append(F(field).asc())
order_fields = base_order
# End of back compat code
if django.VERSION < (4, 1):
base_order = []
for field in order_fields:
if isinstance(field, Expression):
base_order.append(field)
elif isinstance(field, str):
if field[0] == "-":
base_order.append(F(field[1:]).desc())
else:
base_order.append(F(field).asc())
order_fields = base_order

# Get the rank table query
rank_table_query = self.query.get_rank_table_query()
Expand Down

0 comments on commit 2fcbc05

Please sign in to comment.