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
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.
- Vue.js For loop is not rendering content
- Concatenating filter requests with a loop in django
- Django url – No reverse match
- User_passes_test: raise exception if user does not pass
- How to set non-model field property to all the objects in ManyToMany Relation?
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
- How to copy an object's field to another object's field
- Why do I get RemovedInDjango110Warning when redirecting to log in page lite this?
- Add db_index = True to a Django model which already has a lot of data
Source:stackexchange.com