Create List of Blank Forms on Submit

41 Views Asked by At

I am working with Django and have one form for each employee that expires every two weeks. For example, last weeks timesheets are now expired and I need to create a list of new timesheets.

How can I create multiple blank forms, based on creation of new time period?

Context:


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)

Employee Timesheet example:

class Foo(model.Models)
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
    time_in = ###
    time_out = ###

How can I use CreateNewTimesheet, let’s say 14 (each employee) blank forms using model Foo for each employee in order to input new time, for each employee, for the next two week period, without having to create each individual timesheet manually?

1

There are 1 best solutions below

0
dafrandle On

Do this on the view function:

class NewPayPeriodCreate(CreateView): # class based view
    model = TwoWeekPeriod
    template_name = "form.html"
    form_class = TwoWeekPeriodForm
    success_url = reverse_lazy('app_name:success_url_name')

    def form_valid(self, form):  # override the action that occurs when valid form data has been POSTed.
        response = super().form_valid(form) # do the default actions of the function, which includes saving the TwoWeekPeriod object

        employee_queryset = Employee.objects.all()
        for employee in employee_queryset:
            CreateNewTimesheet.objects.create(
                employee=employee,
                start_date=self.object.start_date, # access the TwoWeekPeriod object just created with 'self.object'
                end_date=self.object.end_date,
                two_week_period=self.object
            )

        return response

note that this is 1 database query for each employee and so if you have a lot of employees there may be performance considerations.

remove the overridden save in CreateNewTimesheet. Since you need to make multiple CreateNewTimesheet records you cannot start from one of those records - if you wanted to do an overridden save you would do it in TwoWeekPeriod.

Just note that bulk actions do not use a model's save function.