bhanuprakash
bhanuprakash

Reputation: 149

How to store values from one model to another model in django?

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)

class History(models.Model):

    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()

    def __str__(self):
        return self.name

I want to store the values name and employee_ID from model Leave into the model History only after the status == 1. I'm pretty new to django, help me with this.

Upvotes: 0

Views: 3109

Answers (1)

Umair Mohammad
Umair Mohammad

Reputation: 4635

If you want to relate a model to another model then based on relation you can use OneToOneField, ManyToManyField, ForeignKey, etc

Here what you have done is that you have a model Leave with Fields employee_ID, name and user

Another model History with fields employee_ID, name, user (inherited from Leave) and emp_ID, full_name. This is just extending that model class and creating new model objects. Both will remain unrelated only. (I don't have enough experience to tell you exact/detailed behaviour)

So, for you case you can have a OneToOneField(or something else based on your business logic) from history model to leave model something like this.

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)


class History(models.Model):
    emp_ID = models.CharField(('employee ID'),max_length = 25)
    full_name = models.CharField(('Name'),max_length = 40)
    leave = models.OneToOneField(Leave, on_delete = models.SET_NULL)

Then you can save your data as follows :

# I am assuming you're inside some view and you want to create leave for the user who is logged in, refactor based on your requirements
leave = Leave.objects.create(employee_ID="EMPLOYEEID", name="EMPLOYEENAME", user=request.user)
# here pass the leave that we have created previously
history = History.objects.create(emp_ID="EMPLOYEEID", full_name="EMPLOYEEFULLNAME", leave=leave)

I am assuming you're having leave start and end datetime field also. I think you should also look into why employee ID is repeated in both models.

Let me know if you have any doubts/queires. Thanks~

Upvotes: 1

Related Questions