[Django] #29702: QuerySet database pattern regexp support

11 views
Skip to first unread message

Django

unread,
Aug 22, 2018, 10:05:57 AM8/22/18
to django-...@googlegroups.com
#29702: QuerySet database pattern regexp support
-----------------------------------------+------------------------
Reporter: ddalex | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
Currently there is no API to match a string against regex patterns stored
in a database. Proposing an API to enable this functionality.

== Current regex API

The current set of QuerySet APIs supports regexp functions (i.e `__regex`
and `__iregex`) support passing in a pattern that is matched against a
field in the database. E.g. for a database `value_field=CharField()`
containing the `foo`, `bar` and `for`, the queryset API
`Q(value__regexp='.o.')` will match `foo` and `for` through a `value_field
REGEX 'o'` WHERE clause.

== New API proposal

What is missing is an API to allow the regex (proposing `__revregex` and
`__irevregex`) pattern to be stored in the field and to apply a WHERE
clause on a string that returns pattern-matching entries in the database.

E.G have the database field `pattern=CharField()` that stores a regex
pattern, with the values '.o.' and '.*'.
The queryset statement `Q(pattern__revregex='foo')` will match both
values, while the pattern `Q(pattern__revregex)='bar')` will match only
the `.*` value.

== Example

Currently I'm implementing this with an `extra()` query:

{{{

class DisplayClassifiers(models.Model):
regex_pattern = models.CharField(max_length=10, null=False)

....

# return my classifiers that match the string supplied

DisplayClassifiers.objects.extra(where=["'{0}' REGEXP
regex_pattern".format(regex_pattern.replace("'", ""))])
}}}

This generates this SQL:
{{{
SELECT .... FROM `mainapp_displayclassifier` WHERE ('foo-bar-go' REGEXP
regex_pattern)
}}}

Works as expected with mariadb 10.0.31

--
Ticket URL: <https://code.djangoproject.com/ticket/29702>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 22, 2018, 10:06:21 AM8/22/18
to django-...@googlegroups.com
#29702: QuerySet database-side pattern regexp support
-------------------------------+--------------------------------------

Reporter: ddalex | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/29702#comment:1>

Django

unread,
Aug 22, 2018, 10:21:08 AM8/22/18
to django-...@googlegroups.com
#29702: QuerySet database-side pattern regexp support
-------------------------------------+-------------------------------------
Reporter: Alex Damian | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: new feature, | Triage Stage:
queryset, regex | Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Alex Damian):

* keywords: => new feature, queryset, regex
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => New feature


--
Ticket URL: <https://code.djangoproject.com/ticket/29702#comment:2>

Django

unread,
Aug 22, 2018, 10:40:32 AM8/22/18
to django-...@googlegroups.com
#29702: QuerySet database-side pattern regexp support
-------------------------------------+-------------------------------------
Reporter: Alex Damian | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: new feature, | Triage Stage: Accepted
queryset, regex |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/29702#comment:3>

Django

unread,
Aug 22, 2018, 12:23:57 PM8/22/18
to django-...@googlegroups.com
#29702: QuerySet database-side pattern regexp support
-------------------------------------+-------------------------------------
Reporter: Alex Damian | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: new feature, | Triage Stage: Accepted
queryset, regex |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

I'm not sure if this warrants inclusion as a core lookup, this seems like
a rare use case and best suited for a third-party `RegexField` that has
this implements this special lookup and pattern validation?

--
Ticket URL: <https://code.djangoproject.com/ticket/29702#comment:4>

Django

unread,
Mar 25, 2020, 6:54:43 PM3/25/20
to django-...@googlegroups.com
#29702: QuerySet database-side pattern regexp support
-------------------------------------+-------------------------------------
Reporter: Alex Damian | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: new feature, | Triage Stage: Accepted
queryset, regex |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Adam (Chainz) Johnson):

This can also be done with regex database functions [like those provided
by Django-MySQL](https://django-
mysql.readthedocs.io/en/latest/database_functions.html#regexp-functions).
Not sure there's enough similarity across DB's to warrant a cross-DB regex
function, although I guess we already have the lookup.

--
Ticket URL: <https://code.djangoproject.com/ticket/29702#comment:5>

Django

unread,
Mar 12, 2024, 3:25:29 AM3/12/24
to django-...@googlegroups.com
#29702: QuerySet database-side pattern regexp support
-------------------------------------+-------------------------------------
Reporter: Alex Damian | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: new feature, | Triage Stage: Accepted
queryset, regex |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ülgen Sarıkavak):

* cc: Ülgen Sarıkavak (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/29702#comment:6>
Reply all
Reply to author
Forward
0 new messages