A RegexpField that clean the regex match using the desired format

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from django.forms import RegexField

class RegexFormatField(RegexField):
     
    def __init__(self, *args, **kwargs):
        if 'format' in kwargs:
            self.format = kwargs['format']
            del kwargs['format']
        super(RegexFormatField, self).__init__(*args, **kwargs)
        
    def clean(self, value):
        value = super(RegexFormatField, self).clean(value)
        if self.format is not None:
            value = self.format % self.regex.match(value).groupdict()
        return value

More like this

  1. BRPhoneNumberWidget by semente 1 year, 9 months ago
  2. Simple Mobile Support by bahoo 2 years, 8 months ago
  3. Model field choices as a namedtuple by whiteinge 2 years, 2 months ago
  4. Widget for DateTime values on Geraldo Reports by marinho 4 years, 2 months ago
  5. Bitwise operator queryset filter by hgeerts@osso.nl 3 years, 1 month ago

Comments

(Forgotten your password?)