Created
August 15, 2012 22:33
-
-
Save gladson/3364313 to your computer and use it in GitHub Desktop.
Exemplo CNPJ
This file contains 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.forms import ModelForm, TextInput | |
from app.models import Exemplo | |
class ExemploForm(ModelForm): | |
cnpj = CharField(max_length=18) | |
class Meta: | |
model = Exemplo | |
widgets = { | |
'cnpj': TextInput(attrs={'mask': 'cnpj'}), | |
} | |
def clean_cnpj(self): | |
return self.cleaned_data['cnpj'].replace('-','').replace('.','').replace('/','') |
This file contains 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
# Ponha este método dentro da sua Classe do forms.py | |
# Dependência do módulo re (expressão regular) do python | |
import re | |
def clean(self): | |
cleaned_data = self.cleaned_data | |
field_only_digits = ['cpf_cnpj', 'zipcode', 'phone'] | |
for field in field_only_digits: | |
cleaned_data[field] = re.sub("\D", "", cleaned_data[field]) | |
return cleaned_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment