Reputation: 13
I need to creaet records in model Tabel after creation moden Vacation. I have Employee who creates a vacation and each date from the duration of vacation has to go to model Tabel.
name = models.CharField('Name', max_length = 20)
last_name = models.CharField('Last name', max_length = 20)
def __str__(self):
return self.last_name
class Vacation(models.Model):
employee = models.ForeignKey(Employee, on_delete = models.SET_NULL, blank = True, null = True)
vacation_type = models.ForeignKey(VacationType, on_delete = models.SET_NULL, blank = True, null = True)
date_creation = models.DateTimeField(auto_now = True)
start_date = models.DateField()
end_date = models.DateField()
duration = models.DurationField(default=timedelta)
class Tabel(models.Model):
employee = models.ForeignKey(Employee, on_delete = models.SET_NULL, blank = True, null = True)
date = models.DateField()
day_type = models.ForeignKey(TabelType, on_delete = models.SET_NULL, blank = True, null = True)
late_changes = models.BooleanField(default = False)
I just start with Django. What shoud I use to update model Tabel after Vacation creation? Thank you in advance for any advice!
Upvotes: 1
Views: 1213
Reputation: 4432
As mentioned in comment, you can user Django post_save signal:
models.py or signals.py
from django.db.models.signals import post_save
@receiver(post_save, sender=Vacation)
def create_tabel(sender, instance, created, **kwargs):
if created:
Tabel.objects.create(employee=instance.employee)
Another option is to override Vocation
save method:
def save(self, *args, **kwargs):
is_new = True if not self.id else False
super(Book, self).save(*args, **kwargs)
if is_new:
Tabel.objects.create(employee=self.employee)
Upvotes: 1