Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#22255 closed Cleanup/optimization (fixed)

RegexValidator needs support for flags passed to `re.compile` (migrations do not allow for compiled patterns)

Reported by: blueyed Owned by: dekomote
Component: Core (Other) Version: master
Severity: Normal Keywords:
Cc: dekomote Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


When using a RegexValidator with a compiled pattern, makemigration fails:

ValueError: Cannot serialize: <_sre.SRE_Pattern object at 0x24e8ac0>

A sample model:

from django.core.validators import RegexValidator
pattern = re.compile(r'foo', re.IGNORECASE)
validator = RegexValidator(pattern)

class Model(models.Model):
    foo = models.CharField(
        validators = [validator],

This is somehow expected, and when passing in the pattern uncompiled it works.
But this won't allow to use re.IGNORE_CASE or other flags for re.compile anymore.

RegexValidator should support a new argument flags, which would then be used for non-compiled patterns (passed on to re.compile).

Let me know, if I could provide a pull request / patch for this (as far as I can see) trivial change.

(It seems like with the new migrations, supporting a pre-compiled pattern is not really relevant anymore (but could be kept still))

(I found this related commit:

Change History (10)

comment:1 Changed 2 years ago by blueyed

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Since you can pass the flag(s) using (?i) etc, this can be easily worked around.

comment:2 Changed 2 years ago by erikr

  • Easy pickings set
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Bug to Cleanup/optimization

So, with the latest comment, are you suggesting you no longer think patching this is useful?

Patching this makes sense to me: it can make code that uses RegexValidator a lot clearer when flags are passed separately, and the change required in Django is trivial.
For clarity: #21275 explains that the serialisation of validators is an intentional feature.

comment:3 Changed 2 years ago by dekomote

  • Owner changed from nobody to dekomote
  • Status changed from new to assigned

comment:4 Changed 2 years ago by dekomote

  • Cc dekomote added
  • Has patch set

comment:6 Changed 2 years ago by blueyed

  • Triage Stage changed from Accepted to Ready for checkin
  • Type changed from Cleanup/optimization to Bug

FWIW, the patch looks good to me.


comment:7 Changed 2 years ago by blueyed

  • Type changed from Bug to Cleanup/optimization

comment:8 Changed 2 years ago by erikr

  • Patch needs improvement set

Left a comment on the patch. Minor issue in the docs.

comment:9 Changed 2 years ago by Erik Romijn <erik@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 4d0c5f61427a8e67552ee2d777fffbadc7aff3b2:

Fixed #22255 -- Added support for specifying re flags in RegexValidator

comment:10 Changed 2 years ago by erikr

  • Patch needs improvement unset

This was also backported to 1.7, thanks for the patch :)

Note: See TracTickets for help on using tickets.
Back to Top