Created
December 7, 2009 08:44
-
-
Save idan/250714 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django import forms | |
from django.utils.translation import ugettext_lazy as _ | |
class HexField(models.CharField): | |
def __init__(self, *args, **kwargs): | |
self.min_length = kwargs.pop('min_length', None) | |
super(HexField, self).__init__(*args, **kwargs) | |
def formfield(self, **kwargs): | |
defaults = { | |
'form_class': forms.RegexField, | |
'regex': '^[A-Fa-f0-9]+$', | |
'max_length': self.max_length, | |
'min_length': self.min_length, | |
'error_messages': { | |
'invalid': _(u'Enter only valid hexadecimal characters.') | |
} | |
} | |
defaults.update(kwargs) | |
return super(HexField, self).formfield(**defaults) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment