#22255 closed Cleanup/optimization (fixed)
RegexValidator needs support for flags passed to `re.compile` (migrations do not allow for compiled patterns)
Reported by: | Daniel Hahler | Owned by: | Dejan Noveski |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Dejan Noveski | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
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
).
(https://github.com/django/django/blob/master/django/core/validators.py#L25)
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: https://github.com/django/django/commit/e565e1332ddfbb44fe7e6139375e3c243af7398d)
Change History (10)
comment:1 by , 11 years ago
comment:2 by , 11 years ago
Easy pickings: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → 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 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 11 years ago
Cc: | added |
---|---|
Has patch: | set |
Pull request here https://github.com/django/django/pull/2427
comment:6 by , 11 years ago
Triage Stage: | Accepted → Ready for checkin |
---|---|
Type: | Cleanup/optimization → Bug |
FWIW, the patch looks good to me.
Thanks!
comment:7 by , 11 years ago
Type: | Bug → Cleanup/optimization |
---|
comment:8 by , 11 years ago
Patch needs improvement: | set |
---|
Left a comment on the patch. Minor issue in the docs.
comment:9 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:10 by , 11 years ago
Patch needs improvement: | unset |
---|
This was also backported to 1.7, thanks for the patch :)
Since you can pass the flag(s) using
(?i)
etc, this can be easily worked around.