简体   繁体   English

如何在 django 的 Replace function 中使用正则表达式

[英]How can I use regex in django's Replace function

I'm trying to update all urls in my query by using django's update and Replace function using regex.我正在尝试使用 django 的update查询中的所有 url,并使用正则表达式Replace function。 Here's what I've tried so far but it seems like django's Value expression did not recognize my regex.这是我到目前为止尝试过的,但似乎 django 的Value表达式无法识别我的正则表达式。

from django.db.models import Value
from django.db.models.functions import Replace

Foo.objects.filter(
    some_url__iregex=r'^\/some-link\/\d+\/').update(
    some_url=Replace(
        'some_url', Value(r'^\/some-link\/\d+\/'),
        Value('/some-link/')))

My goal is to remove all numbers after /some-link/ (eg /some-link/55/test to just /some-link/test )我的目标是删除/some-link/之后的所有数字(例如/some-link/55/test到只是/some-link/test

I wasn't able to get the Replace function from Django working, but I was able to use the Function for REGEXP_REPLACE detailed in this answer: https://stackoverflow.com/a/68402017/3056056 I wasn't able to get the Replace function from Django working, but I was able to use the Function for REGEXP_REPLACE detailed in this answer: https://stackoverflow.com/a/68402017/3056056

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM