With the former implementation, I have code ready to go. The latter, I
would definitely want some guidance.
This is of course all assuming that this feature is desired.
Here is an example usage:
{{{#!python
MyModel.objects.annotate(no_letters=RegexpReplace(F('name'), r'[A-Za-z]+',
''))
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28805>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Matthew Schinckel):
Personally, I'd create it in the {{{django.contrib.postgres}}} section:
there are already other functions in there that you should be able to look
at how they are written.
The other alternative is to put it in
{{{django.db.models.functions.text}}}, but I'm not sure how to flag that
it only works on specific backends there.
You might want to bring this up on the django-developers list, as that
sometimes gets a bit more notice.
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:1>
Comment (by Matthew Schinckel):
Oh, it is worth pointing out that this is something that should be easy to
package up in a reusable manner, since it (probably) won't require any
changes, just the addition of a new class.
That class could then be imported from anywhere.
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:2>
Comment (by Joey Wilhelm):
Yeah, I was debating the thought of creating some sort of `django-
postgres-regex` package, for this and related functions. But if I could
contribute it to core, why not, ya know?
The implementation relatively easy; I based it off, I believe, Substr.
{{{#!python
from django.db.models import Func, Value
class RegexpReplace(Func):
function = 'REGEXP_REPLACE'
def __init__(self, expression, pattern, replacement, **extra):
if not hasattr(pattern, 'resolve_expression'):
if not isinstance(pattern, str):
raise TypeError("'pattern' must be a string")
pattern = Value(pattern)
if not hasattr(replacement, 'resolve_expression'):
if not isinstance(replacement, str):
raise TypeError("'replacement' must be a string")
replacement = Value(replacement)
expressions = [expression, pattern, replacement]
super().__init__(*expressions, **extra)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:3>
* stage: Unreviewed => Accepted
Comment:
For a mergable patch, I think we would want both Oracle and PostgreSQL
support.
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:4>
* cc: Sardorbek Imomaliev (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:5>
* cc: Oskar Persson (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:6>
* status: new => assigned
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
* owner: nobody => Nick Pope
* needs_docs: 0 => 1
* has_patch: 0 => 1
Comment:
I have a WIP [https://github.com/django/django/pull/12438 PR].
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:7>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
* needs_docs: 1 => 0
Comment:
I've updated the [https://github.com/django/django/pull/12438 PR] to add
support for `RegexpStrIndex`, `RegexpReplace`, and `RegexpSubstr`.
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:8>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:9>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:10>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:11>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:12>
* needs_better_patch: 0 => 1
Comment:
Moving back to PNI. Having a rethink on a few aspects of this patch.
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:13>
Comment (by Mariusz Felisiak):
You can also remove code for MySQL <= 8.0.4.
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:14>
Comment (by Nick Pope):
Replying to [comment:14 Mariusz Felisiak]:
> You can also remove code for MySQL <= 8.0.4.
Yup. Already done in what I'm working on.
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:15>
* cc: Oskar Persson (removed)
--
Ticket URL: <https://code.djangoproject.com/ticket/28805#comment:16>