简体   繁体   中英

Django : Updating model by overriding save method

This is my first model

from django.db import models
from django.contrib.auth.models import User

CHOICES = (('Earned Leave','Earned Leave'),('Casual Leave','Casual Leave'),('Sick Leave','Sick Leave'),('Paid Leave','Paid Leave'))
STATUS_CHOICES = (('0', 'Rejected'),('1', 'Accepted'),)


class Leave(models.Model):

    employee_ID = models.CharField(max_length = 20)
    name = models.CharField(max_length = 50)
    user = models.ForeignKey(User, on_delete = models.CASCADE, null =True)
    type_of_leave = models.CharField(max_length = 15, choices = CHOICES)
    from_date = models.DateField()
    to_date = models.DateField()
    status = models.CharField(max_length = 15, choices = STATUS_CHOICES)

    @property
    def date_diff(self):
        return (self.to_date - self.from_date).days

This is my second model

class History(models.Model):

    first_name = models.CharField(max_length = 50)
    last_name = models.CharField(max_length = 50)
    employee_ID = models.CharField(max_length = 20)
    earned_leave = models.IntegerField()
    casual_leave = models.IntegerField()
    sick_leave = models.IntegerField()
    paid_leave =models.IntegerField()

Here upon saving the first model Leave , I have written to override save method like,

def save(self, *args, **kwargs):
        super(Leave, self).save()
        if  self.employee_ID == History.employee_ID:
            if self.status == '1':
                if self.type_of_leave == 'Earned Leave':

                    history = History.objects.update(
                earned_leave = self.date_diff,  
                )

But upon saving the first model, all the entries in the History model are getting updated. Where in the history table every user have a separate entry with user's details( first_name, last_name, employee_ID ) and default values as 10 for the rest. Upon saving the Leave model only the entry that is associated with the employee_ID of Leave model should be updated in the History model. For that purpose i have given as if self.employee_ID == History.employee_ID: but it isn't working.

I've even tried as -

def save(self, *args, **kwargs):
            super(Leave, self).save()
            if  self.employee_ID == History.employee_ID:
                if self.status == '1':
                    if self.type_of_leave == 'Earned Leave':

                        history = History.objects.update(
                    earned_leave = History.earned_leave - self.date_diff,  
                    )

But this is not working, nothing gets updated and get'a an error unsupported operand types

So, the base of the project is employee-leave-management. As the user applies for the leave and is accepted the number of days left should get updated in History table or model.

If there's any alternate method, share.

History.objects.update(...) does in fact update all the History objects. You should specify which ones you want to update in your query:

from django.db.models import F

def save(self, *args, **kwargs):
    super(Leave, self).save()
    if self.status == '1':
        if self.type_of_leave == 'Earned Leave':
            history = History.objects.filter(employee_id=self.employee_id).update(
                earned_leave = F('earned_leave') - self.date_diff,  
            )

The F() expression here refers to the value of the column and will be computed by the database rather than in Python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM