I have a below model Student. In Django Admin can I write a code so that when user types the value of "ActualFees" it should check if "BalanceFees" is empty . If it is empty then Paidfees should get copied in BalanceFees otherwise don't copy.
Again if "PaidFees" is typed by user then "BalanceFees" must be calculated as,
BalanceFees = BalanceFees - PaidFees.
How to write code to achive this in Django Admin?
class Student(models.Model):
GENDER = (
('M', 'Male'),
('F', 'Female'),)
BLOOD = (
('O+','O+'),
('O-','O-'),
('A +','A +'),
('A -','A -'),
('B +','B +'),
('B -','B -'),
('AB +','AB +'),
('AB -','AB -'),
)
enroll_no = models.IntegerField()
Name = models.CharField(max_length=200,help_text='SurName MiddleName First name ')
gender = models.CharField(max_length=1,blank = True,choices=GENDER)
blood_gr = models.CharField(max_length=3,blank = True,choices=BLOOD)
category = models.CharField(max_length=200,blank = True)
photo = models.ImageField(upload_to="static",blank = True)
ActualFees = models.IntegerField(blank = True)
PaidFees = models.IntegerField(blank = True, null = True)
BalanceFees = models.IntegerField(blank = True, null = True)
PermenantAddress = models.TextField(blank = True)
Parents_Mobile = models.IntegerField(blank = True,default=0)
Personal_Mobile = models.IntegerField(blank = True,default=0)
LocalAddress = models.TextField(blank = True)
Parents_email = models.CharField(max_length=200,blank = True,default='')
Personal_email = models.CharField(max_length=200,blank = True,default='')
def __str__(self):
return self.Name
There are two ways to approach this:
AdminForm
and then if one field is empty calculating the appropriate other value, with validations to make sure all three fields sum to the right value. The issue with this approach is that if a client turns of javascript they can still enter invalid dataonblur
event.A mix of both is probably best, and the actual implementation is left as an exercise to the reader.
To add extra scripts in, its as easy as modifying the
Media
class: