Django shift table Shows only the facilities to which you belong

Since the total number of staff exceeds 20, it will be difficult to see if the shift table is displayed for facilities that are not related to me, so I corrected it. I had a lot of trouble with this fix, and I asked a question on Qiita and was able to implement it: point_up:

schedule/views.py


from django.shortcuts import render, redirect, HttpResponseRedirect
from shisetsu.models import *
from accounts.models import *
from .models import *
import calendar
import datetime
from datetime import timedelta
from datetime import datetime as dt
from django.db.models import Sum, Q
from django.contrib.auth.models import User
from django.views.generic import FormView, UpdateView, ListView, CreateView, DetailView, DeleteView
from django.urls import reverse_lazy
from .forms import *
from dateutil.relativedelta import relativedelta
from django.contrib.auth.models import User
from django.contrib.auth.mixins import LoginRequiredMixin

# Create your views here.

def homeschedule(request):
    from datetime import datetime
    now = datetime.now()
    return HttpResponseRedirect('/schedule/monthschedule/%s/%s/' % (now.year,now.month,))  #Automatically redirect to this month's shift screen

def monthschedulefilterfunc(request,year_num,month_num,shisetsu_num):
    #user_list = User.objects.all()
    #profile_list = Profile.objects.select_related().values().order_by('hyoujijyun')
    #Get affiliation information from logged-in user
    current_user = request.user
    UserShozoku_list = UserShozokuModel.objects.filter(user = current_user).values_list("shisetsu_name", flat=True)
    UserShozoku_list = list(UserShozoku_list)
    #Get the same affiliation information as the logged-in user
    Shzoku_list = UserShozokuModel.objects.filter(shisetsu_name__in = UserShozoku_list).values_list("user_id", flat=True).distinct()
   
    user_list = User.objects.select_related().all().order_by("profile__hyoujijyun")
    profile_list = Profile.objects.filter(user_id__in = Shzoku_list).select_related().values().order_by("hyoujijyun")
    
    
    year, month, shisetsu = int(year_num), int(month_num), int(shisetsu_num)
    shisetsu_object = Shisetsu.objects.filter(id = shisetsu)
    shisetsu_all_object = Shisetsu.objects.all()
    shift_object = Shift.objects.all()
    object_list = Schedule.objects.filter(year = year, month = month).order_by('user', 'date')
    month_total = Schedule.objects.select_related('User').filter(year = year, month = month).values("user").order_by("user").annotate(month_total_worktime = Sum("day_total_worktime"))
    #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) 
    kikan = str(startdate) +"~"+ str(enddate)
    
    #Create a list of dates and days of the week
    hiduke = str(startdate)
    date_format = "%Y-%m-%d"
    hiduke = dt.strptime(hiduke, date_format)
    weekdays = ["Month","fire","water","wood","Money","soil","Day"]
    calender_object = []
    youbi_object = []
    for i in range(kaisu):
        hiduke = hiduke + timedelta(days=1)    
        calender_object.append(hiduke)
        youbi = weekdays[hiduke.weekday()] 
        youbi_object.append(youbi)
    
    kaisu = str(kaisu)

    context = {
        'year': year,
        'month': month,
        'kikan': kikan,
        'object_list': object_list,
        'user_list': user_list,
        'shift_object': shift_object,
        'calender_object': calender_object,
        'youbi_object': youbi_object,
        'kaisu': kaisu,
        'shisetsu_object': shisetsu_object,
        'month_total' : month_total,
        'profile_list' : profile_list,
        'shisetsu_all_object' : shisetsu_all_object,
    }
    return render(request,'schedule/monthfilter.html', context)

def monthschedulefunc(request,year_num,month_num):
    #Get affiliation information from logged-in user
    current_user = request.user
    UserShozoku_list = UserShozokuModel.objects.filter(user = current_user).values_list("shisetsu_name", flat=True)
    UserShozoku_list = list(UserShozoku_list)
    #Get the same affiliation information as the logged-in user
    Shzoku_list = UserShozokuModel.objects.filter(shisetsu_name__in = UserShozoku_list).values_list("user_id", flat=True).distinct()
    
    user_list = User.objects.select_related().all().order_by("profile__hyoujijyun")
    profile_list = Profile.objects.filter(user_id__in = Shzoku_list).select_related().values().order_by("hyoujijyun")

    #Reacquired for facility button display
    UserShozoku_list = UserShozokuModel.objects.filter(user = current_user)

    year, month = int(year_num), int(month_num)
    shisetsu_object = Shisetsu.objects.all()
    shift_object = Shift.objects.all()
    object_list = Schedule.objects.filter(year = year, month = month).order_by('user', 'date')
    month_total = Schedule.objects.select_related('User').filter(year = year, month = month).values("user").order_by("user").annotate(month_total_worktime = Sum("day_total_worktime"))
    #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) 
    kikan = str(startdate) +"~"+ str(enddate)
    
    #Create a list of dates and days of the week
    hiduke = str(startdate)
    date_format = "%Y-%m-%d"
    hiduke = dt.strptime(hiduke, date_format)
    weekdays = ["Month","fire","water","wood","Money","soil","Day"]
    calender_object = []
    youbi_object = []
    for i in range(kaisu):
        hiduke = hiduke + timedelta(days=1)    
        calender_object.append(hiduke)
        youbi = weekdays[hiduke.weekday()] 
        youbi_object.append(youbi)
    
    kaisu = str(kaisu)

    context = {
        'year': year,
        'month': month,
        'kikan': kikan,
        'object_list': object_list,
        'user_list': user_list,
        'shift_object': shift_object,
        'calender_object': calender_object,
        'youbi_object': youbi_object,
        'kaisu': kaisu,
        'shisetsu_object': shisetsu_object,
        'month_total' : month_total,
        'profile_list' : profile_list,
        'UserShozoku_list':UserShozoku_list,
    }
    return render(request,'schedule/month.html', context)

def scheduleUpdatefunc(request,pk):
    Schedule_list = Schedule.objects.get(pk = int(pk))
    User_list = User.objects.get(username = Schedule_list.user)
    shift_object = Shift.objects.all()
    
    if request.method == 'POST':
        form = ScheduleUpdateForm(data=request.POST)
        year = Schedule_list.year
        month = Schedule_list.month
        #Update button processing
        if form.is_valid():
            Schedule_list.shift_name_1 = form.cleaned_data['shift_name_1']
            Schedule_list.shisetsu_name_1 = form.cleaned_data['shisetsu_name_1']
            Schedule_list.shift_name_2 = form.cleaned_data['shift_name_2']
            Schedule_list.shisetsu_name_2 = form.cleaned_data['shisetsu_name_2']
            Schedule_list.shift_name_3 = form.cleaned_data['shift_name_3']
            Schedule_list.shisetsu_name_3 = form.cleaned_data['shisetsu_name_3']
            Schedule_list.shift_name_4 = form.cleaned_data['shift_name_4']
            Schedule_list.shisetsu_name_4 = form.cleaned_data['shisetsu_name_4']
            #Update button
            if "updatebutton" in request.POST:
                Schedule_list.day_total_worktime = form.cleaned_data['day_total_worktime']
            #Calculate from shift and update button
            elif "sumupdatebutton" in request.POST:
                #Calculate total daily work hours from shifts
                sum_work_time = 0
                for shift in shift_object:
                    if str(form.cleaned_data['shift_name_1']) == str(shift.name):
                        sum_work_time = shift.wrok_time + sum_work_time
                    if str(form.cleaned_data['shift_name_2']) == str(shift.name):
                        sum_work_time = shift.wrok_time + sum_work_time
                    if str(form.cleaned_data['shift_name_3']) == str(shift.name):
                        sum_work_time = shift.wrok_time + sum_work_time
                    if str(form.cleaned_data['shift_name_4']) == str(shift.name):
                        sum_work_time = shift.wrok_time + sum_work_time
                
                Schedule_list.day_total_worktime = sum_work_time
        Schedule_list.save()
        return HttpResponseRedirect('/schedule/monthschedule/%s/%s/' % (year,month,))       
    else:
        item = {
            "shift_name_1":Schedule_list.shift_name_1,
            "shisetsu_name_1": Schedule_list.shisetsu_name_1,
            "shift_name_2": Schedule_list.shift_name_2,
            "shisetsu_name_2": Schedule_list.shisetsu_name_2,
            "shift_name_3": Schedule_list.shift_name_3,
            "shisetsu_name_3": Schedule_list.shisetsu_name_3,
            "shift_name_4": Schedule_list.shift_name_4,
            "shisetsu_name_4": Schedule_list.shisetsu_name_4,
            "day_total_worktime": Schedule_list.day_total_worktime,
            }
        form = ScheduleUpdateForm(initial=item)
        context = {
            'form' : form,
            'Schedule_list': Schedule_list,
            'User_list': User_list,
            'shift_object': shift_object,
        }
        return render(request,'schedule/update.html', context )

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,))
            
def kiboulistfunc(request):
    current_user = request.user
    if current_user.is_superuser == True: #For superuser, show everything in the list
        KibouShift_list = KibouShift.objects.all().order_by('-date')
    else: #General users display only their own records.
         KibouShift_list = KibouShift.objects.filter(user = current_user.id).order_by('-date')
    User_list = User.objects.all()
    shift_object = Shift.objects.all()
    context = {
        'KibouShift_list': KibouShift_list,
        'User_list': User_list,
        'shift_object': shift_object,
        }

    return render(request,'schedule/kiboushift/list.html', context )
        
class KibouCreate(CreateView):
    template_name = 'schedule/kiboushift/create.html'
    model = KibouShift
    fields = ('user', 'date', 'shift_name_1', 'shisetsu_name_1', 'shift_name_2', 'shisetsu_name_2', 'shift_name_3', 'shisetsu_name_3', 'shift_name_4', 'shisetsu_name_4')
    success_url = reverse_lazy('schedule:KibouList')

def kiboubCreate(request):
    if request.method == 'POST':
        if now.day > 5:
            startdate = datetime.date(now.year,now.month,20)
            if date < startdate:
                raise ValidationError(
                "It is a date that cannot be entered",
                params={'value': value},
                )
        else:
            startdate = datetime.date(now.year,now.month,20)
            startdate = enddate + relativedelta(months=1)
            if date < startdate:
                raise ValidationError(
                "It is a date that cannot be entered",
                params={'value': value},
                )
        return date
    

class KibouUpdate(UpdateView):
    template_name = 'schedule/kiboushift/update.html'
    model = KibouShift
    fields = ('user', 'date', 'shift_name_1', 'shisetsu_name_1', 'shift_name_2', 'shisetsu_name_2', 'shift_name_3', 'shisetsu_name_3', 'shift_name_4', 'shisetsu_name_4')
    def date(self):
        date = self.cleaned_date.get('date')
        now = datetime.now()
        print(now.date)
        #Enter only after 20th when 5th
        if now.day > 5:
            startdate = datetime.date(now.year,now.month,20)
            if date < startdate:
                raise ValidationError(
                "It is a date that cannot be entered",
                params={'value': value},
                )
        else:
            startdate = datetime.date(now.year,now.month,20)
            startdate = enddate + relativedelta(months=1)
            if date < startdate:
                raise ValidationError(
                "It is a date that cannot be entered",
                params={'value': value},
                )
        return date

    success_url = reverse_lazy('schedule:KibouList')


class KibouDelete(DeleteView):
    template_name = 'schedule/kiboushift/delete.html'
    model = KibouShift
    fields = ('user', 'date', 'shift_name_1', 'shisetsu_name_1', 'shift_name_2', 'shisetsu_name_2', 'shift_name_3', 'shisetsu_name_3', 'shift_name_4', 'shisetsu_name_4')
    success_url = reverse_lazy('schedule:KibouList')

def dayschedulefunc(request,date):
    profile_list = Profile.objects.select_related().values().order_by('hyoujijyun')
    shisetsu_all_object = Shisetsu.objects.all()
    date = str(date)
    tdatetime = datetime.datetime.strptime(date, '%Y-%m-%d')
    tdate = datetime.date(tdatetime.year, tdatetime.month, tdatetime.day)
    object_list = Schedule.objects.select_related().filter(date = tdate)
    shift_object = Shift.objects.all()

    start_time = datetime.time(15, 00)
    for object in object_list:
        print(object.shift_name_1.start_time)

    context = {
    'profile_list': profile_list,
    'shisetsu_all_object': shisetsu_all_object,
    'object_list': object_list,
    'shift_object':shift_object,
    }
    return render(request,'schedule/gant.html', context)

schedule/month.html


{% extends 'accounts/base.html' %}
{% load static %}
{% block customcss %}
<link rel="stylesheet" href="{% static 'schedule/month.css' %}">
{% endblock customcss %}

{% block header %}
    <div class="header">
        <div class="cole-md-1">
                <a href="{% url 'schedule:KibouList' %}" class="btn-secondary btn active">List of desired shifts</a></p>
                {% ifnotequal month 1 %}
                    <a href="{% url 'schedule:monthschedule' year month|add:'-1' %}" class="btn-info btn active">last month</a>
                {% else %}
                    <a href="{% url 'schedule:monthschedule' year|add:'-1' 12 %}" class="btn-info btn active">last month</a>
                {% endifnotequal %}
       
                {% ifnotequal month 12 %}
                    <a href="{% url 'schedule:monthschedule' year month|add:'1' %}" class="btn-info btn active">Next month</a>
                {% else %}
                     <a href="{% url 'schedule:monthschedule' year|add:'1' 1 %}" class="btn-info btn active">Next month</a>
                {% endifnotequal %}
                {% if perms.schedule.add_schedule %}<!--Authority-->
                    <a href="{% url 'schedule:schedulecreate' year month %}" class="btn-info btn active">Shift creation</a>
                {% endif %}
        </div>
        <div class="cole-md-2">
            {% for shift in shift_object %}
                {% if shift.name != "Closed" and shift.name != "Yes" %}
                    {{ shift.name }} : {{ shift.start_time | date:"G"}}~{{ shift.end_time | date:"G"}} 
                {% endif %}
            {% endfor %}    
        </div>
        <p>
            <a href="{% url 'schedule:monthschedule' year month %}" button type="button" class="btn btn-outline-dark">all</a>
            {% for shisetsu in shisetsu_object %}
                {% for UserShozoku in UserShozoku_list %}
                    {% if shisetsu.name|stringformat:"s" == UserShozoku.shisetsu_name|stringformat:"s" %}
                        <a href="{% url 'schedule:monthschedulefilter' year month shisetsu.pk %}" button type="button" class="btn btn-outline-dark" span style="background-color:{{ shisetsu.color }}">{{ shisetsu.name }}</span></a>
                    {% endif %}
                {% endfor %}
            {% endfor %}
        </p>
    </div> 

{% endblock header %}

{% block content %}
<table class="table">
    <thead>
        <tr>    <!--date-->
            <th class ="fixed00" rowspan="2">{{ kikan }}</th>
            {% for item in calender_object %}
            <th class ="fixed01">{{ item.date | date:"d" }}</th>
            {% endfor %}
        <tr>   <!--Day of the week-->
            {% for item in youbi_object %}
                <th class ="fixed02">{{ item }}</th>
            {% endfor %}
        </tr>
    </thead>
    <tbody>
    {% for profile in profile_list %}
        {% for staff in user_list %}
            {% if profile.user_id == staff.id %}
                <tr align="center">
                <th class ="fixed03" >{{ staff.last_name }} {{ staff.first_name }}</th>  <!--staff_id element used in js-->
                {% for item in object_list %} 
                    {% if item.user|stringformat:"s" == staff.username|stringformat:"s" %}<!--If the username is the same-->
                        <td class="meisai"> 
                        {% if item.shift_name_1 != None %}
                            {% if item.shift_name_1|stringformat:"s" == "Yes" or item.shift_name_1|stringformat:"s" == "Closed" %}
                                {{ item.shift_name_1 }}
                            {% else %}
                                {% for shisetsu in shisetsu_object %}
                                    {% if item.shisetsu_name_1|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                        <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_1 }}</span>
                                    {% endif %}
                                {% endfor %} 
                            {% endif %}    
                        {% endif %}
                {% if item.shift_name_2 != None %}
                    {% if item.shift_name_2|stringformat:"s" == "Yes" or item.shift_name_2|stringformat:"s" == "Closed" %}
                        {{ item.shift_name_2 }}
                    {% else %}
                        {% for shisetsu in shisetsu_object %}
                            {% if item.shisetsu_name_2|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                    <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_2 }}</span>
                            {% endif %}
                        {% endfor %} 
                    {% endif %}    
                {% endif %}
                {% if item.shift_name_3 != None %}
                    {% if item.shift_name_3|stringformat:"s" == "Yes" or item.shift_name_3|stringformat:"s" == "Closed" %}
                        {{ item.shift_name_3 }}
                    {% else %}
                        {% for shisetsu in shisetsu_object %}
                            {% if item.shisetsu_name_3|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_3 }}</span>
                            {% endif %}
                        {% endfor %} 
                    {% endif %}    
                {% endif %}
                {% if item.shift_name_4 != None %}
                    {% if item.shift_name_4|stringformat:"s" == "Yes" or item.shift_name_4|stringformat:"s" == "Closed" %}
                        {{ item.shift_name_4 }}
                    {% else %}
                        {% for shisetsu in shisetsu_object %}
                            {% if item.shisetsu_name_4|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_4 }}</span>
                            {% endif %}
                        {% endfor %} 
                    {% endif %}    
                {% endif %}                   
            {% endif %}            
        {% endfor %}
        </td>

        <tr align="center">
            {% for month in month_total %} 
                {% if month.user ==  staff.id %}<!--If the username is the same-->
                    <td class="fixed04"><b>{{ month.month_total_worktime }}</b></td>
                {% endif %}
            {% endfor %}
            {% for item in object_list %} 
                {% if item.user|stringformat:"s" == staff.username|stringformat:"s" %}<!--If the username is the same-->
                    {% if perms.schedule.add_schedule %}<!--Authority-->
                        <td  class="meisai" id="s{{ staff.id }}d{{ item.date }}">
                            <a href="{% url 'schedule:update' item.pk %}">{{ item.day_total_worktime }} </a>
                        </td>
                    {% else %}
                        <td  class="meisai" id="s{{ staff.id }}d{{ item.date }}">
                            {{ item.day_total_worktime }}
                        </td>
                    {% endif %}
                {% endif %}            
            {% endfor %}
        </tr>
        {% endif %}
        {% endfor %}
        {% endfor %}
        </tbody>
    </table>
</div>
</div>
{% endblock content %}

schedule/monthfileter.html


{% extends 'accounts/base.html' %}
{% load static %}
{% block customcss %}
<link rel="stylesheet" href="{% static 'schedule/month.css' %}">
{% endblock customcss %}

{% block header %}
    <div class="header">
        <div class="cole-md-1">
                <a href="{% url 'schedule:KibouList' %}" class="btn-secondary btn active">List of desired shifts</a></p>
                {% ifnotequal month 1 %}
                    <a href="{% url 'schedule:monthschedule' year month|add:'-1' %}" class="btn-info btn active">last month</a>
                {% else %}
                    <a href="{% url 'schedule:monthschedule' year|add:'-1' 12 %}" class="btn-info btn active">last month</a>
                {% endifnotequal %}
       
                {% ifnotequal month 12 %}
                    <a href="{% url 'schedule:monthschedule' year month|add:'1' %}" class="btn-info btn active">Next month</a>
                {% else %}
                     <a href="{% url 'schedule:monthschedule' year|add:'1' 1 %}" class="btn-info btn active">Next month</a>
                {% endifnotequal %}
                {% if perms.schedule.add_schedule %}<!--Authority-->
                    <a href="{% url 'schedule:schedulecreate' year month %}" class="btn-info btn active">Shift creation</a>
                {% endif %}
        </div>
        <div class="cole-md-2">
            {% for shift in shift_object %}
                {% if shift.name != "Closed" and shift.name != "Yes" %}
                    {{ shift.name }} : {{ shift.start_time | date:"G"}}~{{ shift.end_time | date:"G"}} 
                {% endif %}
            {% endfor %}    
        </div>
        <p>
            <a href="{% url 'schedule:monthschedule' year month %}" button type="button" class="btn btn-outline-dark">all</a>
            {% for shisetsu in shisetsu_object %}
                {% for UserShozoku in UserShozoku_list %}
                    {% if shisetsu.name|stringformat:"s" == UserShozoku.shisetsu_name|stringformat:"s" %}
                        <a href="{% url 'schedule:monthschedulefilter' year month shisetsu.pk %}" button type="button" class="btn btn-outline-dark" span style="background-color:{{ shisetsu.color }}">{{ shisetsu.name }}</span></a>
                    {% endif %}
                {% endfor %}
            {% endfor %}
        </p>
    </div>

     

{% endblock header %}

{% block content %}
<table class="table">
    <thead>
        <tr>    <!--date-->
            <th class ="fixed00" rowspan="2">{{ kikan }}</th>
            {% for item in calender_object %}
                <th class ="fixed01">{{ item.date | date:"d" }}</th>
            {% endfor %}
        <tr>   <!--Day of the week-->
            {% for item in youbi_object %}
                <th class ="fixed02">{{ item }}</th>
            {% endfor %}
        </tr>
    </thead>
    <tbody>
    {% for profile in profile_list %}
        {% for staff in user_list %}
            {% if profile.user_id == staff.id %}
                <tr align="center">
                <th class ="fixed03" >{{ staff.last_name }} {{ staff.first_name }}</th>  <!--staff_id element used in js-->
                {% for item in object_list %} 
                    {% if item.user|stringformat:"s" == staff.username|stringformat:"s" %}<!--If the username is the same-->
                        <td class="meisai"> 
                        {% if item.shift_name_1 != None %}
                            {% if item.shift_name_1|stringformat:"s" == "Yes" or item.shift_name_1|stringformat:"s" == "Closed" %}
                                {{ item.shift_name_1 }}
                            {% else %}
                                {% for shisetsu in shisetsu_object %}
                                    {% if item.shisetsu_name_1|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                        <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_1 }}</span>
                                    {% endif %}
                                {% endfor %} 
                            {% endif %}    
                        {% endif %}
                {% if item.shift_name_2 != None %}
                    {% if item.shift_name_2|stringformat:"s" == "Yes" or item.shift_name_2|stringformat:"s" == "Closed" %}
                        {{ item.shift_name_2 }}
                    {% else %}
                        {% for shisetsu in shisetsu_object %}
                            {% if item.shisetsu_name_2|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                    <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_2 }}</span>
                            {% endif %}
                        {% endfor %} 
                    {% endif %}    
                {% endif %}
                {% if item.shift_name_3 != None %}
                    {% if item.shift_name_3|stringformat:"s" == "Yes" or item.shift_name_3|stringformat:"s" == "Closed" %}
                        {{ item.shift_name_3 }}
                    {% else %}
                        {% for shisetsu in shisetsu_object %}
                            {% if item.shisetsu_name_3|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_3 }}</span>
                            {% endif %}
                        {% endfor %} 
                    {% endif %}    
                {% endif %}
                {% if item.shift_name_4 != None %}
                    {% if item.shift_name_4|stringformat:"s" == "Yes" or item.shift_name_4|stringformat:"s" == "Closed" %}
                        {{ item.shift_name_4 }}
                    {% else %}
                        {% for shisetsu in shisetsu_object %}
                            {% if item.shisetsu_name_4|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_4 }}</span>
                            {% endif %}
                        {% endfor %} 
                    {% endif %}    
                {% endif %}                   
            {% endif %}            
        {% endfor %}
        </td>

        <tr align="center">
            {% for month in month_total %} 
                {% if month.user ==  staff.id %}<!--If the username is the same-->
                    <td class="fixed04"><b>{{ month.month_total_worktime }}</b></td>
                {% endif %}
            {% endfor %}
            {% for item in object_list %} 
                {% if item.user|stringformat:"s" == staff.username|stringformat:"s" %}<!--If the username is the same-->
                    {% if perms.schedule.add_schedule %}<!--Authority-->
                        <td  class="meisai" id="s{{ staff.id }}d{{ item.date }}">
                            <a href="{% url 'schedule:update' item.pk %}">{{ item.day_total_worktime }} </a>
                        </td>
                    {% else %}
                        <td  class="meisai" id="s{{ staff.id }}d{{ item.date }}">
                            {{ item.day_total_worktime }}
                        </td>
                    {% endif %}
                {% endif %}            
            {% endfor %}
        </tr>
        {% endif %}
        {% endfor %}
        {% endfor %}
        </tbody>
    </table>
</div>
{% endblock content %}

If you belong to some facilities

image.png

If you belong to all facilities image.png

It worked pretty well (⌒∇⌒)

What I thought this time was that I couldn't understand the condition specification for data extraction at all. I don't know the SQL statement either, but I don't understand Django's data extraction so much that I thought that I could get the data I wanted by executing it with SQL and extracting the data.

I can't find a good site, and I'm likely to hit a wall, but I'll push forward.

Recommended Posts

Django shift table Shows only the facilities to which you belong
Django Make choices only for the facility you belong to
I want to scroll the Django shift table, but ...
Added a function to register desired shifts in the Django shift table
Display the Django shift table! Supports 20-day closing
I tried to create a table only with Django
Django Python shift table
[Django] Carefully explain the escape route if you really want to use the table of another application
Deploy the Django tutorial to IIS ①
How to make only one data register on the Django admin screen
From Django 1.8, you can pass a callable object to the choices attribute of forms.ChoiceField, which is very convenient.