Skip to content

Commit 49f6425

Browse files
committed
Try approach with substr instead of replace
1 parent 03ce54a commit 49f6425

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

readthedocs/redirects/querysets.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Queryset for the redirects app."""
22

33
from django.db import models
4-
from django.db.models import Value, CharField, Q, F, Func
4+
from django.db.models import Value, CharField, Q, F, ExpressionWrapper
5+
from django.db.models.functions import Substr, Length
56

67
from readthedocs.core.utils.extend import SettingsOverrideObject
78

@@ -38,14 +39,16 @@ def get_redirect_path_with_status(self, path, full_path=None, language=None, ver
3839
output_field=CharField(),
3940
),
4041

41-
# NOTE: using replace here could take some time if there are a lot
42-
# of redirect for this project.
43-
from_url_without_rest=Func(
44-
F('from_url'),
45-
Value('$rest'),
46-
Value(''),
47-
# This could be done with ``Replace`` once on Django 2.2
48-
function='replace',
42+
from_length=ExpressionWrapper(
43+
Length('from_url'),
44+
output_field=CharField()
45+
),
46+
47+
# 1-indexed
48+
from_url_without_rest=Substr(
49+
'from_url',
50+
1,
51+
F('from_length') - 5 # Strip "$rest"
4952
),
5053
)
5154
prefix = Q(

0 commit comments

Comments
 (0)