[Fixed]-Unsupported operand type(s) for -: 'int' and 'unicode' in django models

1👍

You’ve given your method the same name as a field, judging by the calculation, you can just remove the field. Dont forget to makemigrations.

Alternatively, rename the method

👤Sayse

0👍

just remove the item_remaining function and write save function in that place

def save(self):
    self.item_remaining = self.quantity - self.sold

This save function will be called whenever an object is saved. So the item_remaining field will be updated whenever object is saved.

0👍

Or you can delete the field and make the item_remaining a property.
Now, you can use item_remaining as "instance.item_remaining".

class Inv(models.Model):
    description = models.CharField(max_length = 32)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    quantity = models.IntegerField()
    sold = models.IntegerField( default=0)

    def __str__(self):
        return self.description

    @property 
    def item_remaining(self):
        return self.quantity - self.sold

Leave a comment