[PYTHON] Django shift creation function Added basic work shift registration function for days of the week

① Shift table: Only the administrator can edit the time. ② Shift table I want you to copy the shift creation by day of the week ③ Desired shift: By the 5th of every month, we ask for a shift request and create and distribute it by the 10th, so input restrictions are required to enter by 5. ④ Bulletin board: Notify each facility ⑤ Task It is best if you can take over what you have to do

Since ① has been completed, it is ② that we started.

I created a new BaseShift table with user information and registered the basic shift there.

schedule/models.py


class BaseShift(models.Model):
    id = models.AutoField(verbose_name='Desired shift ID',primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='Employee name')
    getsu_shift_name_1 = models.ForeignKey(Shift, verbose_name='Month_1 shift name', related_name='base_shift_getsu_shift_name_1',on_delete=models.SET_NULL,null= True)
    getsu_shisetsu_name_1 = models.ForeignKey(Shisetsu, verbose_name='Month_1 facility', related_name='base_shift_getsu_shisetsu_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    getsu_shift_name_2 = models.ForeignKey(Shift, verbose_name='Month_2 shift name', related_name='base_shift_getsu_shift_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    getsu_shisetsu_name_2 = models.ForeignKey(Shisetsu, verbose_name='Month_2 facilities', related_name='base_shift_getsu_shisetsu_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    getsu_shift_name_3 = models.ForeignKey(Shift, verbose_name='Month_3 shift name', related_name='base_shift_getsu_shift_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    getsu_shisetsu_name_3 = models.ForeignKey(Shisetsu, verbose_name='Month_3 facilities', related_name='base_shift_getsu_shisetsu_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    getsu_shift_name_4 = models.ForeignKey(Shift, verbose_name='Month_4 shift name', related_name='base_shift_getsu_shift_name_4',on_delete=models.SET_NULL,blank=True, null=True)
    getsu_shisetsu_name_4 = models.ForeignKey(Shisetsu, verbose_name='Month_4 facilities', related_name='base_shift_getsu_shisetsu_name_4',on_delete=models.SET_NULL,blank=True, null=True)

    ka_shift_name_1 = models.ForeignKey(Shift, verbose_name='fire_1 shift name', related_name='base_shift_ka_shift_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    ka_shisetsu_name_1 = models.ForeignKey(Shisetsu, verbose_name='fire_1 facility', related_name='base_shift_ka_shisetsu_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    ka_shift_name_2 = models.ForeignKey(Shift, verbose_name='fire_2 shift name', related_name='base_shift_ka_shift_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    ka_shisetsu_name_2 = models.ForeignKey(Shisetsu, verbose_name='fire_2 facilities', related_name='base_shift_ka_shisetsu_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    ka_shift_name_3 = models.ForeignKey(Shift, verbose_name='fire_3 shift name', related_name='base_shift_ka_shift_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    ka_shisetsu_name_3 = models.ForeignKey(Shisetsu, verbose_name='fire_3 facilities', related_name='base_shift_ka_shisetsu_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    ka_shift_name_4 = models.ForeignKey(Shift, verbose_name='fire_4 shift name', related_name='base_shift_ka_shift_name_4',on_delete=models.SET_NULL,blank=True, null=True)
    ka_shisetsu_name_4 = models.ForeignKey(Shisetsu, verbose_name='fire_4 facilities', related_name='base_shift_ka_shisetsu_name_4',on_delete=models.SET_NULL,blank=True, null=True)

    sui_shift_name_1 = models.ForeignKey(Shift, verbose_name='water_1 shift name', related_name='base_shift_sui_shift_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    sui_shisetsu_name_1 = models.ForeignKey(Shisetsu, verbose_name='water_1 facility', related_name='base_shift_sui_shisetsu_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    sui_shift_name_2 = models.ForeignKey(Shift, verbose_name='water_2 shift name', related_name='base_shift_sui_shift_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    sui_shisetsu_name_2 = models.ForeignKey(Shisetsu, verbose_name='water_2 facilities', related_name='base_shift_sui_shisetsu_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    sui_shift_name_3 = models.ForeignKey(Shift, verbose_name='water_3 shift name', related_name='base_shift_sui_shift_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    sui_shisetsu_name_3 = models.ForeignKey(Shisetsu, verbose_name='water_3 facilities', related_name='base_shift_sui_shisetsu_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    sui_shift_name_4 = models.ForeignKey(Shift, verbose_name='water_4 shift name', related_name='base_shift_sui_shift_name_4',on_delete=models.SET_NULL,blank=True, null=True)
    sui_shisetsu_name_4 = models.ForeignKey(Shisetsu, verbose_name='water_4 facilities', related_name='base_shift_sui_shisetsu_name_4',on_delete=models.SET_NULL,blank=True, null=True)

    moku_shift_name_1 = models.ForeignKey(Shift, verbose_name='wood_1 shift name', related_name='base_shift_moku_shift_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    moku_shisetsu_name_1 = models.ForeignKey(Shisetsu, verbose_name='wood_1 facility', related_name='base_shift_moku_shisetsu_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    moku_shift_name_2 = models.ForeignKey(Shift, verbose_name='wood_2 shift name', related_name='base_shift_moku_shift_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    moku_shisetsu_name_2 = models.ForeignKey(Shisetsu, verbose_name='wood_2 facilities', related_name='base_shift_moku_shisetsu_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    moku_shift_name_3 = models.ForeignKey(Shift, verbose_name='wood_3 shift name', related_name='base_shift_moku_shift_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    moku_shisetsu_name_3 = models.ForeignKey(Shisetsu, verbose_name='wood_3 facilities', related_name='base_shift_moku_shisetsu_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    moku_shift_name_4 = models.ForeignKey(Shift, verbose_name='wood_4 shift name', related_name='base_shift_moku_shift_name_4',on_delete=models.SET_NULL,blank=True, null=True)
    moku_shisetsu_name_4 = models.ForeignKey(Shisetsu, verbose_name='wood_4 facilities', related_name='base_shift_moku_shisetsu_name_4',on_delete=models.SET_NULL,blank=True, null=True)

    kin_shift_name_1 = models.ForeignKey(Shift, verbose_name='Money_1 shift name', related_name='base_shift_kin_shift_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    kin_shisetsu_name_1 = models.ForeignKey(Shisetsu, verbose_name='Money_1 facility', related_name='base_shift_kin_shisetsu_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    kin_shift_name_2 = models.ForeignKey(Shift, verbose_name='Money_2 shift name', related_name='base_shift_kin_shift_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    kin_shisetsu_name_2 = models.ForeignKey(Shisetsu, verbose_name='Money_2 facilities', related_name='base_shift_kin_shisetsu_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    kin_shift_name_3 = models.ForeignKey(Shift, verbose_name='Money_3 shift name', related_name='base_shift_kin_shift_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    kin_shisetsu_name_3 = models.ForeignKey(Shisetsu, verbose_name='Money_3 facilities', related_name='base_shift_kin_shisetsu_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    kin_shift_name_4 = models.ForeignKey(Shift, verbose_name='Money_4 shift name', related_name='base_shift_kin_shift_name_4',on_delete=models.SET_NULL,blank=True, null=True)
    kin_shisetsu_name_4 = models.ForeignKey(Shisetsu, verbose_name='Money_4 facilities', related_name='base_shift_kin_shisetsu_name_4',on_delete=models.SET_NULL,blank=True, null=True)

    do_shift_name_1 = models.ForeignKey(Shift, verbose_name='soil_1 shift name', related_name='base_shift_do_shift_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    do_shisetsu_name_1 = models.ForeignKey(Shisetsu, verbose_name='soil_1 facility', related_name='base_shift_do_shisetsu_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    do_shift_name_2 = models.ForeignKey(Shift, verbose_name='soil_2 shift name', related_name='base_shift_do_shift_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    do_shisetsu_name_2 = models.ForeignKey(Shisetsu, verbose_name='soil_2 facilities', related_name='base_shift_do_shisetsu_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    do_shift_name_3 = models.ForeignKey(Shift, verbose_name='soil_3 shift name', related_name='base_shift_do_shift_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    do_shisetsu_name_3 = models.ForeignKey(Shisetsu, verbose_name='soil_3 facilities', related_name='base_shift_do_shisetsu_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    do_shift_name_4 = models.ForeignKey(Shift, verbose_name='soil_4 shift name', related_name='base_shift_do_shift_name_4',on_delete=models.SET_NULL,blank=True, null=True)
    do_shisetsu_name_4 = models.ForeignKey(Shisetsu, verbose_name='soil_4 facilities', related_name='base_shift_do_shisetsu_name_4',on_delete=models.SET_NULL,blank=True, null=True)

    nichi_shift_name_1 = models.ForeignKey(Shift, verbose_name='Day_1 shift name', related_name='base_shift_nichi_shift_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    nichi_shisetsu_name_1 = models.ForeignKey(Shisetsu, verbose_name='Day_1 facility', related_name='base_shift_nichi_shisetsu_name_1',on_delete=models.SET_NULL,blank=True, null=True)
    nichi_shift_name_2 = models.ForeignKey(Shift, verbose_name='Day_2 shift name', related_name='base_shift_nichi_shift_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    nichi_shisetsu_name_2 = models.ForeignKey(Shisetsu, verbose_name='Day_2 facilities', related_name='base_shift_nichi_shisetsu_name_2',on_delete=models.SET_NULL,blank=True, null=True)
    nichi_shift_name_3 = models.ForeignKey(Shift, verbose_name='Day_3 shift name', related_name='base_shift_nichi_shift_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    nichi_shisetsu_name_3 = models.ForeignKey(Shisetsu, verbose_name='Day_3 facilities', related_name='base_shift_nichi_shisetsu_name_3',on_delete=models.SET_NULL,blank=True, null=True)
    nichi_shift_name_4 = models.ForeignKey(Shift, verbose_name='Day_4 shift name', related_name='base_shift_nichi_shift_name_4',on_delete=models.SET_NULL,blank=True, null=True)
    nichi_shisetsu_name_4 = models.ForeignKey(Shisetsu, verbose_name='Day_4 facilities', related_name='base_shift_nichi_shisetsu_name_4',on_delete=models.SET_NULL,blank=True, null=True)

I don't know if it's better to make it like this, but if you don't follow the previous method, it may affect other things, so it's been a long time, but I made it with this method.

schedule/views.py


def schedulecreatefunc(request,year_num,month_num):
    year, month = int(year_num), int(month_num)
    #Delete all schedules of the target year and month
    Schedule.objects.filter(year = year, month = month).delete()
    #Get the number of days in the shift range
    enddate = datetime.date(year,month,20)
    startdate = enddate + relativedelta(months=-1)
    #Extract the hope of the target period from the desired shift
    kiboushift_list = KibouShift.objects.filter(date__range=[startdate, enddate])
    kiboushift_list = list(kiboushift_list)
    shift_list = Shift.objects.all()
    
    #Reflect the desired shift
    for kibou in kiboushift_list:
        sum_work_time = 0
        for shift in shift_list:
                if str(kibou.shift_name_1) == str(shift.name):
                    sum_work_time = shift.wrok_time + sum_work_time
                if str(kibou.shift_name_2) == str(shift.name):
                    sum_work_time = shift.wrok_time + sum_work_time
                if str(kibou.shift_name_3) == str(shift.name):
                    sum_work_time = shift.wrok_time + sum_work_time
                if str(kibou.shift_name_4) == str(shift.name):
                    sum_work_time = shift.wrok_time + sum_work_time
        new_object = Schedule(
            user = kibou.user,
            date = kibou.date, 
            year = year,
            month = month,
            shift_name_1 = kibou.shift_name_1, 
            shisetsu_name_1 = kibou.shisetsu_name_1,
            shift_name_2 = kibou.shift_name_2, 
            shisetsu_name_2 = kibou.shisetsu_name_2,
            shift_name_3 = kibou.shift_name_3, 
            shisetsu_name_3 = kibou.shisetsu_name_3,
            shift_name_4 = kibou.shift_name_4, 
            shisetsu_name_4 = kibou.shisetsu_name_4,
            day_total_worktime = sum_work_time,
            )
        new_object.save()
    
    #Create other than desired shift
    user_list = User.objects.all()
    #Get the number of days in the shift range
    enddate = datetime.date(year,month,20)
    startdate = enddate + relativedelta(months=-1)
    kaisu = enddate - startdate
    kaisu = int(kaisu.days) 
    #last month
    if month != 1 :
        zengetsu = month - 1
    else:
        zengetsu = 12
        year = year - 1
    #Process for each user
    for user in user_list:
        #base_Created from the information where shift is registered
        base_shift = BaseShift.objects.filter(user = user.id)
        kongetsu_list = Schedule.objects.filter(year = year, month = month, user = user.id).order_by('date')
        zengetsu_list = Schedule.objects.filter(year = year, month = zengetsu, user = user.id).order_by('date')
        sakusei_date = startdate
        shift_list = Shift.objects.all()
        for new_shift in range(kaisu):
            sakusei_date = sakusei_date + timedelta(days=1)
            if kongetsu_list.filter(user = user.id, date = sakusei_date).exists():
                print("ok")                       
            else:
                weekdays = ["Month","fire","water","wood","Money","soil","Day"]
                youbi = weekdays[sakusei_date.weekday()] 

                #Get from base shift and get
                if base_shift.filter(user = user.id ).exists():
                    for base in base_shift:
                        sum_work_time = 0
                        shift1 = None
                        shisetsu1 = None
                        shift2 = None
                        shisetsu2 = None
                        shift3 = None
                        shisetsu3 = None
                        shift4 = None
                        shisetsu4 = None

                        if youbi == "Month":
                            for shift in shift_list:
                                if str(base.getsu_shift_name_1) == str(shift.name):
                                    shift1 = base.getsu_shift_name_1
                                    shisetsu1 = base.getsu_shisetsu_name_1
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.getsu_shift_name_2) == str(shift.name):
                                    shift2 = base.getsu_shift_name_2
                                    shisetsu2 = base.getsu_shisetsu_name_2
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.getsu_shift_name_3) == str(shift.name):
                                    shift3 = base.getsu_shift_name_3
                                    shisetsu3 = base.getsu_shisetsu_name_3
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.getsu_shift_name_4) == str(shift.name):
                                    shift4 = base.getsu_shift_name_4
                                    shisetsu4 = base.getsu_shisetsu_name_4
                                    sum_work_time = shift.wrok_time + sum_work_time
                        
                        elif youbi == "fire":
                            for shift in shift_list:
                                if str(base.ka_shift_name_1) == str(shift.name):
                                    shift1 = base.ka_shift_name_1
                                    shisetsu1 = base.ka_shisetsu_name_1
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.ka_shift_name_2) == str(shift.name):
                                    shift2 = base.ka_shift_name_2
                                    shisetsu2 = base.ka_shisetsu_name_2
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.ka_shift_name_3) == str(shift.name):
                                    shift3 = base.ka_shift_name_3
                                    shisetsu3 = base.ka_shisetsu_name_3
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.ka_shift_name_4) == str(shift.name):
                                    shift4 = base.ka_shift_name_4
                                    shisetsu4 = base.ka_shisetsu_name_4
                                    sum_work_time = shift.wrok_time + sum_work_time
                    
                        elif youbi == "water":
                            for shift in shift_list:
                                if str(base.sui_shift_name_1) == str(shift.name):
                                    shift1 = base.sui_shift_name_1
                                    shisetsu1 = base.sui_shisetsu_name_1
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.sui_shift_name_2) == str(shift.name):
                                    shift2 = base.sui_shift_name_2
                                    shisetsu2 = base.sui_shisetsu_name_2
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.sui_shift_name_3) == str(shift.name):
                                    shift3 = base.sui_shift_name_3
                                    shisetsu3 = base.sui_shisetsu_name_3
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.sui_shift_name_4) == str(shift.name):
                                    shift4 = base.sui_shift_name_4
                                    shisetsu4 = base.sui_shisetsu_name_4
                                    sum_work_time = shift.wrok_time + sum_work_time

                        elif youbi == "wood":
                            for shift in shift_list:
                                if str(base.moku_shift_name_1) == str(shift.name):
                                    shift1 = base.moku_shift_name_1
                                    shisetsu1 = base.moku_shisetsu_name_1
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.moku_shift_name_2) == str(shift.name):
                                    shift2 = base.moku_shift_name_2
                                    shisetsu2 = base.moku_shisetsu_name_2
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.moku_shift_name_3) == str(shift.name):
                                    shift3 = base.moku_shift_name_3
                                    shisetsu3 = base.moku_shisetsu_name_3
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.moku_shift_name_4) == str(shift.name):
                                    shift4 = base.moku_shift_name_4
                                    shisetsu4 = base.moku_shisetsu_name_4
                                    sum_work_time = shift.wrok_time + sum_work_time

                        elif youbi == "Money":
                            for shift in shift_list:
                                if str(base.kin_shift_name_1) == str(shift.name):
                                    shift1 = base.kin_shift_name_1
                                    shisetsu1 = base.kin_shisetsu_name_1
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.kin_shift_name_2) == str(shift.name):
                                    shift2 = base.kin_shift_name_2
                                    shisetsu2 = base.kin_shisetsu_name_2
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.kin_shift_name_3) == str(shift.name):
                                    shift3 = base.kin_shift_name_3
                                    shisetsu3 = base.kin_shisetsu_name_3
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.kin_shift_name_4) == str(shift.name):
                                    shift4 = base.kin_shift_name_4
                                    shisetsu4 = base.kin_shisetsu_name_4
                                    sum_work_time = shift.wrok_time + sum_work_time

                        elif youbi == "soil":
                            for shift in shift_list:
                                if str(base.do_shift_name_1) == str(shift.name):
                                    shift1 = base.do_shift_name_1
                                    shisetsu1 = base.do_shisetsu_name_1
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.do_shift_name_2) == str(shift.name):
                                    shift2 = base.do_shift_name_2
                                    shisetsu2 = base.do_shisetsu_name_2
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.do_shift_name_3) == str(shift.name):
                                    shift3 = base.do_shift_name_3
                                    shisetsu3 = base.do_shisetsu_name_3
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.do_shift_name_4) == str(shift.name):
                                    shift4 = base.do_shift_name_4
                                    shisetsu4 = base.do_shisetsu_name_4
                                    sum_work_time = shift.wrok_time + sum_work_time

                        if youbi == "Day":
                            for shift in shift_list:
                                if str(base.nichi_shift_name_1) == str(shift.name):
                                    shift1 = base.nichi_shift_name_1
                                    shisetsu1 = base.nichi_shisetsu_name_1
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.nichi_shift_name_2) == str(shift.name):
                                    shift2 = base.nichi_shift_name_2
                                    shisetsu2 = base.nichi_shisetsu_name_2
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.nichi_shift_name_3) == str(shift.name):
                                    shift3 = base.nichi_shift_name_3
                                    shisetsu3 = base.nichi_shisetsu_name_3
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(base.nichi_shift_name_4) == str(shift.name):
                                    shift4 = base.nichi_shift_name_4
                                    shisetsu4 = base.nichi_shisetsu_name_4
                                    sum_work_time = shift.wrok_time + sum_work_time

                        new_object=Schedule(
                        user = base.user,
                        date = sakusei_date,
                        year = year,
                        month = month,
                        shift_name_1 = shift1, 
                        shisetsu_name_1 = shisetsu1,
                        shift_name_2 = shift2, 
                        shisetsu_name_2 = shisetsu2,
                        shift_name_3 = shift3, 
                        shisetsu_name_3 = shisetsu3,
                        shift_name_4 = shift4, 
                        shisetsu_name_4 = shisetsu4,
                        day_total_worktime = sum_work_time,
                        )
                        new_object.save()
                else:
                    hukushadate = sakusei_date + relativedelta(months=-1)
                    hukusha_list = zengetsu_list.filter(date = hukushadate, user = user.id)
                    if zengetsu_list.filter(date = hukushadate, user = user.id).exists():
                        for hukusha in hukusha_list:
                        #Calculate total daily work hours from shifts
                            sum_work_time = 0
                            for shift in shift_list:
                                if str(hukusha.shift_name_1) == str(shift.name):
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(hukusha.shift_name_2) == str(shift.name):
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(hukusha.shift_name_3) == str(shift.name):
                                    sum_work_time = shift.wrok_time + sum_work_time
                                if str(hukusha.shift_name_4) == str(shift.name):
                                    sum_work_time = shift.wrok_time + sum_work_time

                            new_object=Schedule(
                                user = hukusha.user,
                                date = sakusei_date,
                                year = year,
                                month = month,
                                shift_name_1 = hukusha.shift_name_1, 
                                shisetsu_name_1 = hukusha.shisetsu_name_1,
                                shift_name_2 = hukusha.shift_name_2, 
                                shisetsu_name_2 = hukusha.shisetsu_name_2,
                                shift_name_3 = hukusha.shift_name_3, 
                                shisetsu_name_3 = hukusha.shisetsu_name_3,
                                shift_name_4 = hukusha.shift_name_4, 
                                shisetsu_name_4 = hukusha.shisetsu_name_4,
                                day_total_worktime = sum_work_time,
                                )
                            new_object.save()
                    else:
                        useradd = User.objects.get(id=user.id)
                        shiftadd = Shift.objects.get(id=2)
                        new_object=Schedule(
                        user = useradd,
                        date = sakusei_date,
                        year = year,
                        month = month,
                        shift_name_1 = shiftadd, 
                        shisetsu_name_1 = None,
                        shift_name_2 = None, 
                        shisetsu_name_2 = None,
                        shift_name_3 = None, 
                        shisetsu_name_3 = None,
                        shift_name_4 = None, 
                        shisetsu_name_4 = None,
                        day_total_worktime = 0,
                        )
                        new_object.save()
    return HttpResponseRedirect('/schedule/monthschedule/%s/%s/' % (year,month,))

Now, if BaseShift is registered, it will be processed first before copying the date of the previous month.

To organize

    1. Handle desired shifts
  1. Process if there is a base shift
    1. Copy the same day of the previous month

This will be the state before editing!

It took a few hours, but I'm glad I was able to confirm the operation safely!

Recommended Posts

Django shift creation function Added basic work shift registration function for days of the week
Added a function to register desired shifts in the Django shift table
Django shift table shift data creation function completed
Numerical representation of days of the week in various languages
[Django] Added new question creation function to polls app
Django shift creation feature
Summary of stumbling blocks in Django for the first time
The image display function of iTerm is convenient for image processing.