1
class Finances(models.Model):
    student_id = models.ForeignKey(Student, on_delete=models.CASCADE)
    student_name = models.CharField(max_length=100, default={Student.student_name})
    total_fees = models.IntegerField(default=3500)
    paid_amount = models.IntegerField(default=0)
    balance = total_fees - paid_amount
    balance_fees = models.IntegerField({balance})

I need the balance_fees model to hold the value of a subtraction of total_fees - paid_amount which is held by the balance. I get an error when making migrations 'TypeError: unsupported operand type(s) for -: 'IntegerField' and 'IntegerField'

How can I resolve this?

2 Answers 2

3

Do you have several options:

Option 1: A python property

class Finances(models.Model):
    student_id = models.ForeignKey(Student, on_delete=models.CASCADE)
    student_name = models.CharField(max_length=100, ... )
    total_fees = models.IntegerField(default=3500)
    paid_amount = models.IntegerField(default=0)
    @property
    def balance_fees(self):
        return self.total_fees - self.paid_amount
  • Pros: is always up-to-date.
  • Cons: not index friendly (you can't index database by this field because is not on database)

Option 2: A django model property update overriding save:

class Finances(models.Model):
    student_id = models.ForeignKey(Student, on_delete=models.CASCADE)
    student_name = models.CharField(max_length=100, ... )
    total_fees = models.IntegerField(default=3500)
    paid_amount = models.IntegerField(default=0)
    balance_fees = models.IntegerField(default=0)
    def save(self, *args, **kwargs):
        self.balance_fees = self.total_fees - self.paid_amount
        super().save(*args, **kwargs)
  • Pros: index friendly, you can include this field on your queries.
  • Cons: only up-to-date after save.

Option 3: A django model property updated via pre-save signal:

class Finances(models.Model):
    student_id = models.ForeignKey(Student, on_delete=models.CASCADE)
    student_name = models.CharField(max_length=100, ... )
    total_fees = models.IntegerField(default=3500)
    paid_amount = models.IntegerField(default=0)
    balance_fees = models.IntegerField(default=0)

# ---

from django.db.models.signals import pre_save
from django.dispatch import receiver
from myapp.models import Finances


@receiver(pre_save, sender=Finances)
def my_handler(sender, **kwargs):
    sender.balance_fees = sender.total_fees - sender.paid_amount
  • Pros: index friendly, you can include this field on your queries.
  • Cons: only up-to-date after save.
0
0

Create a method onside your model:

    @property
    def  balance(self):
      val = self.total_fees - self.paid_amount
      return val

Remove these two lines on tour model:

 balance = total_fees - paid_amount
 balance_fees = models.IntegerField({balance})

And now you can use balance as a field of your model in your views and template

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.