Skip to content

Instantly share code, notes, and snippets.

@gladson
Created August 15, 2012 22:33
Show Gist options
  • Save gladson/3364313 to your computer and use it in GitHub Desktop.
Save gladson/3364313 to your computer and use it in GitHub Desktop.
Exemplo CNPJ
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('/','')
# 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