Django - CPF or CNPJ

Asked

Viewed 2,358 times

1

Good afternoon Personal.

As a way of learning I am creating a system in Django with Client Registration.

Could someone help me in logic so that in the register I can choose CPF or CNPJ.

My Models are like this currently:

class Cliente(models.Model):
    razao_social = models.CharField(max_length=80)
    nome_fantasia = models.CharField(max_length=80)
    cliente_classificacao = models.ForeignKey(ClienteClassificacao, null=False, blank=False, on_delete=models.PROTECT)
    cliente_situacao = models.ForeignKey(ClienteSituacao, null=False, blank=False, on_delete=models.PROTECT)
    cep = models.CharField(max_length=9)
    endereco = models.CharField(max_length=100)
    numero = models.CharField(max_length=6)
    bairro = models.CharField(max_length=100)
    municipio = models.CharField(max_length=50)
    estado = models.CharField(max_length=100)

    def __str__(self):
        return self.nome_fantasia

and my stem is like this:

class ClienteForm(ModelForm):
    cep = BRZipCodeField(max_length=9)

    class Meta:
        model = Cliente
        fields = ['razao_social', 'nome_fantasia', 'cliente_classificacao', 'cliente_situacao',
                  'cep', 'endereco', 'numero', 'bairro', 'municipio', 'estado']

My intention is at the time of registration select the customer’s rating and as the rating the form already change the field mask.

Grateful

  • 1

    You can do this automatically with ajax, analyzing with an if where up to 11 characters are mapped with the CPF structure (XXX.XXX.XXX-XX), if it exceeds this value, limited to 14 consider the CNPJ formatting (XX.XXX.XXX/XXXX-XX). Remembering that, it is great to put the validation of the calculation to verify these numbers. CPF: https://dicasdeprogramacao.com.br/algoritmo-validar-cpf/ CNPJ: http://www.macoratti.net/alg_cnpj.htm

  • 1

    Please correct the code. Paste the code here, as it comes from the text editor, and use the button {} to format.

  • Okay, thanks for the tip.

  • 1

    @Emilioeijiinaba actually Django does not do this - it has to be done via javascript in the browser, and does not have a ready Django object to generate this code

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.