How to tie this to a specific model

23 Views Asked by At

I am getting this bellow error:

null value in column "two_week_period_id" of relation "timesheets_usertimesheet" violates not-null constraint

Which I understand because the very last null value is not being passed through. Here are my models:

models.py

class TwoWeekPeriod(models.Model):
    start_date = models.DateField(_("Start Date"))
    end_date = models.DateField(_("End Date"))
    week_number = models.IntegerField(_("Week Number"))

class CreateNewTimesheet(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    start_date = models.DateField(_("Start Date"))
    end_date = models.DateField(_("End Date"))
    two_week_period = models.ForeignKey(TwoWeekPeriod, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def save(self, *args, **kwargs):
        week_number = (self.start_date - timezone.now().date()).days // 14 + 1
        two_week_period, _ = TwoWeekPeriod.objects.get_or_create(
            start_date=self.start_date,
            end_date=self.end_date,
            defaults={'week_number': week_number}
        )
        self.two_week_period = two_week_period
        super().save(*args, **kwargs)

The above created a two_week_period as an id for every two weeks entered. For example, 03/10/2024-03/14/2024 is two_week of id 1, next two week will add an id of 2 and so on.

I am trying to pass the two_week_period from CreateNewTimesheet over to:

simplied verion of UserTimesheet

class UserTimesheet(models.Model):
    employee = models.ForeignKey(Employee, models.SET_NULL, blank=True, null=True)
    start_date = models.DateField(_("Start Date"))
    end_date = models.DateField(_("End Date"))
    two_week_period = models.ForeignKey(TwoWeekPeriod, on_delete=models.CASCADE)

It seems without the two_week_period in the Usertimesheet model, I do not get this error. But I want to display the two_week_period, so that I can tie the two weeks to one timesheet (for data and storage records).

Is there a better way to tie the two_week_id? Am I doing any of this wrong?

With out two_week_period = models.ForeignKey(TwoWeekPeriod, on_delete=models.CASCADE) it works just fine, just passes a null value, of course.

Any and all help is appreciated.

1

There are 1 best solutions below

0
ckcAdmin On

I was able to figure this out. I just did this to the UserTimesheet model.

class UserTimesheet(models.Model):
    employee = models.ForeignKey(Employee, models.SET_NULL, blank=True, null=True)
    start_date = models.DateField(_("Start Date"))
    end_date = models.DateField(_("End Date"))
    two_week_period = models.ForeignKey(TwoWeekPeriod, on_delete=models.CASCADE)


     def save(self, *args, **kwargs):
         # Determine the week number for this timesheet
         week_number = (self.start_date - timezone.now().date()).days // 14 + 1
        
         # Find or create the corresponding TwoWeekPeriod instance
         two_week_period, _ = TwoWeekPeriod.objects.get_or_create(
             start_date=self.start_date,
             end_date=self.end_date,
             defaults={'week_number': week_number}
           )
        
         # Assign the TwoWeekPeriod to the timesheet
         self.two_week_period = two_week_period
        
         super().save(*args, **kwargs)