0

I have model like this

class Product(models.Model):
    name = models.CharField(max_length=150)
    stock = models.IntegerField()
    cogs = models.IntegerField()
    def __str__(self):
        return f"{self.name}"

class Transaction(models.Model):
    customer = models.ForeignKey(User, on_delete=models.CASCADE)
    product = models.ManyToManyField(Product)
    purchase_date = models.DateField(auto_now_add=True)
    quantity = models.IntegerField()
    total = models.IntegerField(blank=True)

    def save(self, *args, **kwargs):
        self.total =  self.quantity * self.product.cogs
        super().save(self, *args, **kwargs)
    
    def __str__(self):
        return f"{self.customer}"

my question is How to make Product attribute in this case stock will be dynamically calculated when there is transaction input. Every input in transaction will deduct product.stock which is product.stock - transaction.quantity

I try to automatically calculate product.stock when there is input in transaction

3
  • Please don't do that. Each time you will save the transaction it will deduce. Even if you manage to fix that, it can still go wrong when deleting and adding the same transaction again: determine the total based on all transactions in and all transactions out. Commented Jun 10, 2023 at 13:43
  • assume that transaction is transaction out. Can you give me advice how to do that? Commented Jun 10, 2023 at 15:08
  • wel how will you know how much went in? Otherwise you can only determine the total out. Commented Jun 10, 2023 at 15:16

1 Answer 1

1

You can use the @property decorator in order to create a calculated field, It will be performed each time you call it. the difference between attributes and properties in Python is that attributes are simply data members of an object, while properties are methods that are accessed like attributes but actually perform some computation when called.

class BankAccount:
   def __init__(self, balance, interest_rate):
      self.balance = balance
      self.interest_rate = interest_rate

   @property
   def interest_earned(self):
      return self.balance * self.interest_rate

acct1 = BankAccount(1000, 0.05)
print(acct1.balance)
print(acct1.interest_earned)

Output:

1000
50.0

I believe it's so much better than performing the same calculation each time you add, modify or save any change on the model.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.