Validation with clean method in Django 2.0

Asked

Viewed 45 times

0

In my Model have this method below:

def clean(self, *args, **kwargs):
        valortotal = Venda.objects.filter(id=self.id).aggregate(valortotal=Sum(F('item__produto__valor') * F('item__qtde'), output_field=FloatField()))
        valor_nota = self.valor_nota
        if Decimal(valortotal['valortotal']) < valor_nota():
         raise forms.ValidationError("O  valor do Tipo de Pagamento (dinheiro + cartão) não pode ser maior que o valor total da nota.")
        #raise forms.ValidationError(Decimal(valortotal['valortotal']))    

More while saving the system appears error: inserir a descrição da imagem aqui

Does anyone know any solution to this.

  • The message says you cannot convert a None to a decimal value, probably in your if if Decimal(valortotal['valortotal']) < valor_nota(): the valortotal['valortotal'] is returning None.

  • But strange that I have a similar method that is right in the template: def total value(self): sum = Sale.objects.filter(id=self.id). Aggregate(total value=Sum(F('item__product__value') * F('item__qtde'), output_field=Floatfield()) Return sum['total value']

  • Because in this method there is no invalid return, put a line before the if so: valor_total = valortotal['valortotal'] if valortotal['valortotal'] is not None else 0 and change your if to: if Decimal(valor_total) < valor_nota(): and see what happens.

No answers

Browser other questions tagged

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