As the title says, use the admin page. What I would like to experiment with here is
** Filter appointments by date **
** Follow the relationship in reverse **
is. I felt that all of them were Tutorial, so I tried it after reviewing.
It is each admin.py to play with. Let's look at them in order.
schedule/admin.py
from django.contrib import admin
from schedule.models import WorkTime,MonthShift,StaffSchedule,NgShift,GuestSchedule
#### base classes ####
class DateAdmin(admin.ModelAdmin):
	def schedule_date(self,obj):  #Model object is in obj
		return obj.strfdate()  #Returns what you want to see on the changelist page
	date_hierarchy = 'date'  #Set the filtering function by date object
class TimeTableAdmin(admin.ModelAdmin):
	def time_table(self,obj):
		return obj.strftimetable()
#### main classes ####
###### staff ######
class MonthShiftAdmin(admin.ModelAdmin):
	list_display = ('year','month','groupschedule','completed',)  #Item list or tuple to display on the changelist page.
	list_filter = ('groupschedule',)  #Adds a filter function for the specified item
admin.site.register(MonthShift,MonthShiftAdmin)  #Apply by putting the model for admin in the second argument
class WorkTimeAdmin(TimeTableAdmin):
	list_display = ('title','time_table',)	# time_table is a function from the parent
	list_filter = ('groupschedule',)
admin.site.register(WorkTime,WorkTimeAdmin)
class StaffScheduleAdmin(DateAdmin):
	list_display = ('schedule_date','staff','worktime',)	# schedule_date is a function from the parent
	list_filter = ('staff__groupschedule','date','staff',)  #You can follow the reverse relation with two underscores
admin.site.register(StaffSchedule,StaffScheduleAdmin)
class NgShiftAdmin(DateAdmin):
	list_display = ('staff','schedule_date','get_values')
	def get_values(self,obj):
		return obj.ng_values()
	list_filter = ['date','staff',]
admin.site.register(NgShift,NgShiftAdmin)
###### guest ######
class GuestScheduleAdmin(DateAdmin,TimeTableAdmin):
	list_display = ('schedule_date','guest','time_table',)
	list_filter = ['date','guest',]
admin.site.register(GuestSchedule,GuestScheduleAdmin)
It defines a parent class to save code. Experiment to narrow down items by list_filter. I could see them separately by date, so it would be nice to do something similar when creating a shift table.
staff/admin.py
from django.contrib import admin
from staff.models import Staff
class StaffAdmin(admin.ModelAdmin):
	list_display = ('name','ng_list',)
	list_filter = ('groupschedule',)
	def ng_list(self,obj):
		ngs = ""
		for ng in obj.ngshift_set.all():
			ngs += "%s(%s) ," % ( ng.date,ng.ng_values(), )
		return ngs
admin.site.register(Staff,StaffAdmin)
Reverse relation with xxxx_set was also successful.
guest/admin.py
from django.contrib import admin
from guest.models import Guest
class GuestAdmin(admin.ModelAdmin):
	list_display = ('name','get_schedules',)
	list_filter = ('groupschedule',)
	def get_schedules(self,obj):
		schedules = ""
		for schedule in obj.guestschedule_set.all():
			schedules += "%s(%s) ," % ( schedule.date,schedule.strftimetable(), )
		return schedules
	get_schedules.short_description = 'Schedules'
admin.site.register(Guest,GuestAdmin)
It's not much different from staff / admin.py.
owner/admin.py
from django.contrib import admin
from owner.models import GroupSchedule
class GroupScheduleAdmin(admin.ModelAdmin):
	list_display = ('group','owner',)
admin.site.register(GroupSchedule,GroupScheduleAdmin)
It's getting annoying, so I didn't add it yesterday. I'm not going to use the admin screen in the end, so this is fine.
Recommended Posts