'<' not supported between instances of 'method' and 'method' Django 2.0

Asked

Viewed 53 times

0

I implemented a validation with two methods

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():
        if self.valortotal < self.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']))

and Django returned the following error

inserir a descrição da imagem aqui

Below the methods that return values

def valortotal(self):
        soma = Venda.objects.filter(id=self.id).aggregate(valortotal=Sum(F('item__produto__valor') * F('item__qtde'), output_field=FloatField()))
        return soma['valortotal']

def valor_nota(self, force_insert=False, force_update=False):
        valor_nota = self.valor_dinheiro + self.valor_cartao 
        return valor_nota

The Item Class

class Item(models.Model):
    produto = models.ForeignKey(Produto, on_delete=models.CASCADE, default=None)
    qtde = models.PositiveIntegerField(null=True, blank=False)
    venda = models.ForeignKey(Venda, on_delete=models.CASCADE, default=None)

1 answer

0

The message says that on the line where you make the comparison:

if self.valortotal < self.valor_nota:

You are comparing two instances of methods, so the message "< is not supported between instances of methods" ('<' not supported between instances of 'method' and 'method')

To check this, comment the if and print the type of objects involved in the if, with the command:

print('self.valortotal => ', type(self.valortotal))
print('self.valor => ', type(self.valor))

If they are really methods that return values, then change your if to:

if self.valortotal() < self.valor_nota():
  • when doing: if self.total value() < self.value_note(): it returned this error '<' not supported between instances of 'Nonetype' and 'decimal. Decimal'

  • I edited the question by adding the total value() and note value method

  • This is the last error msg you report is the same as your other question, one of the two values of the comparison is None, comment the if and the command print( self.valortotal() is None , self.valor_nota() is None) And you’ll see that a True on the way out.

Browser other questions tagged

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