Opened 8 years ago

Closed 5 years ago

#13473 closed New feature (invalid)

BRCPFField and BRCNPJField updates for

Reported by: Felipe 'chronos' Prenholato Owned by: Felipe 'chronos' Prenholato
Component: contrib.localflavor Version: master
Severity: Normal Keywords: CPF, CNPJ, localflavorsplit
Cc: semente+djangoproject@…, django-l10n-portuguese@…, semente+django@… Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


In Django 1.1 (or 1.0) are added two fields in for CPF and CNPJ, but this ones not have a similar o models and not normalize data to save in database (look #10895). I started some changes to have model fields, number generators (useful for tests), one 'type' and integration of model fields to form fields for CPF and CNPJ. I adding here a diff of my initial work and a link to my repo at github.

Attachments (1)

ticket13473.patch (80.2 KB) - added by Felipe 'chronos' Prenholato 8 years ago.
Diff of work at against at file upload date. This patch contains changes requested in #13495 about refactoring of localflavor tests.

Download all attachments as: .zip

Change History (14)

comment:1 Changed 8 years ago by Felipe 'chronos' Prenholato


I added attachment:ticket13473.patch containing changes against trunk to have CPF/CNPJ fields in models, that use field of forms, and CPF/CNPJ types and number generators.
CPF/CNPJ types and generators are located at
Models and Forms fields are located at models and forms modules.


# -*- coding: utf-8 -*-
from django.db import models
from import BRCPFField,BRCNPJField

class CPFCNPJModel(models.Model):
    cnpj = BRCNPJField(null=True,blank=True)
    cpf = BRCPFField(null=True,blank=True)

    def __unicode__(self):
        return u"cpf: %s ,  cnpj: %s" % (self.cpf or u'',self.cnpj or u'')

# -*- coding: utf-8 -*-
from django import forms
from models import CPFCNPJModel

class CPFCNPJForm(forms.ModelForm):
    class Meta:
        model = CPFCNPJMode

When you type in rendered form in template any know format for input works, here as example using python shell using as base one CPF/CNPJ generated by number generators:

>>> from webui.forms import CPFCNPJModel as model, CPFCNPJForm as form
>>> from import CPFGenerator,CNPJGenerator
>>> # generate numbers
>>> CPFGenerator()
>>> CNPJGenerator()
>>> # init form in various number formats and check for errors
>>> f = form({'cpf':56146650560,'cnpj':97196641000871})
>>> f.is_valid()
>>> f = form({'cpf':'56146650560','cnpj':'97196641000871'})
>>> f.is_valid()
>>> f = form({'cpf':'561.466.505-60','cnpj':'97.196.641/0008-71'})
>>> f.is_valid()
>>> # Save form
<CPFCNPJModel: cpf: 561.466.505-60 ,  cnpj: 97.196.641/0008-71>
>>> # Show model properties
>>> model.objects.all()
[<CPFCNPJModel: cpf: 561.466.505-60 ,  cnpj: 97.196.641/0008-71>]
>>> m=model.objects.all()[0]
>>> m.cpf,m.cnpj
(CPF('56146650560'), CNPJ('97196641000871'))
>>> str(m.cpf),str(m.cnpj)
('561.466.505-60', '97.196.641/0008-71')
>>> m.cpf.single,m.cnpj.single
(u'56146650560', u'97196641000871')

And here is how is stored in database:

$ m dbshell <<< "select * from webui_cpfcnpjmodel;"
 id |      cnpj      |     cpf     
  2 | 97196641000871 | 56146650560
(1 row)


I'm not created yet unit tests and some translations, etc, but I'll soon, if someone wanna to contribute, show errors, fixes, etc, plz take a look on for last changes.

comment:2 Changed 8 years ago by Felipe 'chronos' Prenholato

Has patch: set
Needs documentation: set
Needs tests: set

comment:3 Changed 8 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

comment:4 Changed 8 years ago by Felipe 'chronos' Prenholato

Needs tests: unset

Added tests, locale translations for pt-BR. This ticket depends of #13495 about localflavor tests refactoring to support various countries.

comment:5 Changed 8 years ago by Guilherme Gondim (semente) <semente@…>

Cc: semente+djangoproject@… added

comment:6 Changed 8 years ago by Guilherme Gondim (semente) <semente@…>

Cc: django-l10n-portugueses@… added; semente+djangoproject@… removed

comment:7 Changed 8 years ago by Guilherme Gondim (semente) <semente@…>

Cc: semente+djangoproject@… django-l10n-portuguese@… added; django-l10n-portugueses@… removed

sorry for the many changes

comment:8 Changed 8 years ago by Felipe 'chronos' Prenholato

added some more tests, fixed validations and default values of fields.

Changed 8 years ago by Felipe 'chronos' Prenholato

Attachment: ticket13473.patch added

Diff of work at against at file upload date. This patch contains changes requested in #13495 about refactoring of localflavor tests.

comment:9 Changed 7 years ago by Jannis Leidel

Please open a separate ticket for translation updates.

comment:10 Changed 7 years ago by Julien Phalip

Severity: Normal
Type: New feature

comment:11 Changed 7 years ago by Guilherme Gondim

Cc: semente+django@… added
Easy pickings: unset
UI/UX: unset

comment:12 Changed 6 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

comment:13 Changed 5 years ago by Aymeric Augustin

Keywords: localflavorsplit added
Resolution: invalid
Status: newclosed

django.contrib.localflavor is now deprecated — see

A repository was created for each localflavor at (Replace with the country code.)

If you're still interested in this ticket, could you create a pull request on that repository?

Sorry for not resolving this issue earlier, and thanks for your input!

Note: See TracTickets for help on using tickets.
Back to Top