Home > Blockchain >  overwrite save django method
overwrite save django method

Time:11-18

I need to override Django's method save so when the purchase is made by a certain cpf, it is saved with "Approved" status. Can someone help me?

Follow the models.py


class Purchases(TimeStampedModel):
    APROVADO = "AP"
    EM_VALIDACAO = "VA"
    STATUS_CHOICHES = (
        (APROVADO, "Aprovado"),
        (EM_VALIDACAO, "Em validação"),
    )
    values = models.DecimalField(decimal_places=2, max_digits=10, default=0)
    cpf = BRCPFField("CPF")
    status = models.CharField(max_length=20, choices=STATUS_CHOICHES, default=EM_VALIDACAO)

CodePudding user response:

You just need to add a new method

def save(self, *args, **kwargs):
    #your business logic

    super().save(*args, **kwargs)

CodePudding user response:

def save(self, *args, **kwargs):
    if self.cpf == "15350946056":
        self.status = "AP"
        super(Purchases, self).save(*args, **kwargs)
    else:
        self.status = "VA"
        super(Purchases, self).save(*args, **kwargs)

The CPF is between " " because the BRCPFField deals with charfild, we need to transform the cpf into a string so that there is a comparison

  • Related