[PYTHON] Web App Development Practice: Create a Shift Creation Page with Django! (Experiment on admin page)

Before touching the actual page, I would like to check if the model I made is really okay.

As the title says, use the admin page. What I would like to experiment with here is

  1. ** Filter appointments by date **

  2. ** 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.

Next time, I will do "Write a template for the schedule table".

Recommended Posts

Web App Development Practice: Create a Shift Creation Page with Django! (Experiment on admin page)
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Web App Development Practice: Create a Shift Creation Page with Django! (Introduction)
Web App Development Practice: Create a Shift Creation Page with Django! (Write a base template)
Web App Development Practice: Create a Shift Creation Page with Django! (Design of database model)
Create a Todo app with Django ③ Create a task list page
Create a simple web app with flask
Create a Todo app with Django ④ Implement folder and task creation functions
WEB application development using Django [Admin screen creation]
Deploy a Python 3.6 / Django / Postgres web app on Azure
Create a Todo app with Django REST Framework + Angular
Looking back on creating a web service with Django 1
Create a Todo app with the Django REST framework
Looking back on creating a web service with Django 2
Deploy a Django app made with PTVS on Azure
Create a Todo app with Django ⑤ Create a task editing function
Create a homepage with django
Web application creation with Django
(Failure) Deploy a web app made with Flask on heroku
Create a web API that can deliver images with Django
Create a Todo app with Django ① Build an environment with Docker
Implement a Django app on Hy
Publish DJango page on heroku: Practice
Build a web application with Django
Building a development environment with Maven on Google App Engine [Java]
Launched a web application on AWS with django and changed jobs
Create a file uploader with Django
Create a web app that can be easily visualized with Plotly Dash
"Trash classification by image!" App creation diary day3 ~ Web application with Django ~
Play like a web app with ipywidgets
Create a GUI app with Python's Tkinter
Daemonize a Python web app with Supervisor
Create your first app with Django startproject
A note on enabling PostgreSQL with Django
Create a web service with Docker + Flask
I made a WEB application with Django
[kotlin] Create an app that recognizes photos taken with a camera on android
Try creating a web application with Vue.js and Django (Mac)-(1) Environment construction, application creation
Django Tutorial (Blog App Creation) ① --Preparation, Top Page Creation
How to develop a cart app with Django
Vienna with Python + Flask web app on Jenkins
Create a PDF file with a random page size
Create a page that loads infinitely with python
[Python] Build a Django development environment with Docker
Create a dashboard for Network devices with Django!
Build a Django development environment with Doker Toolbox
Create a new page in confluence with Python
How to create a multi-platform app with kivy
Create a one-file hello world application with django
Until you create a new app in Django
Create a Python virtual development environment on Windows
Extract data from a web page with Python
I tried to create a table only with Django
Build a Django development environment using pyenv-virtualenv on Mac
What is a dog? Django App Creation Start Volume--startapp
Create a native GUI app with Py2app and Tkinter
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
Create a comfortable Python 3 (Anaconda) development environment on windows
Create a python development environment with vagrant + ansible + fabric
Deploy a Django application on Google App Engine (Python3)