简体   繁体   中英

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. Here's what I've tried so far but it seems like django's Value expression did not recognize my regex.

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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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