0

I'm trying to perform the both operations at the same time but only one is getting returned at a time. If I remove either of them, the other one get performed. Is there any way round to get both of them working at the same time? I'm talking about grossannualsalary and textDeduct.

class Salarie(models.Model):

    grossmonthlysalary = models.FloatField(
        _('Monthly Salary'),
        null=False,
        blank=False,
        default=0.00
    )

    monthlyincometax = models.FloatField(
        _('Monthly Income Tax'),
        null=False,
        blank=False
    )

    contractperiod = models.IntegerField(
        _('Contract Period'),
        null=False,
        blank=False
    )

    grossannualsalary = models.FloatField(
        _('Gross Annual Salary'),
        null=True,
        blank=True
    )

    taxDeduct = models.FloatField(
        _('Tax Deduction'),
        null=False,
        blank=False,
        default=0.00
    )

    def save(self, *args, **kwargs):
        self.grossannualsalary = self.grossmonthlysalary * self.contractperiod
        super(Salarie, self).save(*args, **kwargs)

    def save(self, *args, **kwargs):
        self.taxDeduct = self.grossmonthlysalary * self.monthlyincometax / 100
        super(Salarie, self).save(*args, **kwargs)
das-g
  • 9,718
  • 4
  • 38
  • 80
  • 3
    Why is `grossannualsalary` a field if it is always the result of a calculation? why not just make it a property? The same goes for `taxDeduct` – Sayse Dec 22 '16 at 10:22
  • 2
    And why have you defined the save method twice? You can only have one method for a name. Why not put them both in the same function? – Daniel Roseman Dec 22 '16 at 10:23

1 Answers1

2

I can't understand why you write two save method in one model. Please try this.

def save(self, *args, **kwargs):
    self.grossannualsalary = self.grossmonthlysalary * self.contractperiod
    self.taxDeduct = self.grossmonthlysalary * self.monthlyincometax / 100
    super(Salarie, self).save(*args, **kwargs)
Darshit
  • 420
  • 2
  • 11
  • This still only does a calculation "one at a time", its not exactly clear (to me) what the op is actually trying to do – Sayse Dec 22 '16 at 10:26
  • I agree that if grossannualsalary and taxDeduct are derived from calcuation of other model field than it's better to write property. – Darshit Dec 22 '16 at 10:42
  • @Darshit would love to use property in this situation, could you please assist me transform this code to a property calculation. – Israel Z. Kollie Dec 22 '16 at 13:08
  • @IsraelZ.Kollie please go through [this](https://www.stavros.io/posts/how-replace-django-model-field-property/) tutorial and [SO answer](http://stackoverflow.com/questions/1454727/do-properties-work-on-django-model-fields) – Darshit Dec 22 '16 at 15:01