Create a model for your Django schedule

We will create an app to create a shift table.

terminal


python3 manage.py startapp schedule

Add the app to your settings.

config/settings.py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts.apps.AccountsConfig',
    'shisetsu',
    'colorfield',
    'schedule'
]

We will create a table in the created schedule app.

Shifts should be managed so that A is from 9:00 to 18:00. You may also be able to take it as if you were paid. Therefore, I will try to have it in the text field.

Regarding the schedule, it means that you may enter multiple shifts a day and work at multiple facilities. It has never happened to enter a shift of 3 or more.

Based on that, I will try to make it with the following table.

schedule/models.py


from django.db import models
from shisetsu.models import *
 from accounts.models import *

# Create your models here.

class Shift(models.Model):
    id = models.IntegerField(verbose_name='Shift ID',primary_key=True)
    name = models.CharField(verbose_name='Shift name', max_length=1)
    start_time = models.TimeField(verbose_name="Start time")
    end_time = models.TimeField(verbose_name="ending time")
    wrok_time = models.IntegerField(verbose_name='Working hours')

    def __str__(self):
        return self.name

class Schedule(models.Model):
    id = models.IntegerField(verbose_name='Schedule ID',primary_key=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    date = models.DateField(verbose_name='date')
    shift_id_1= models.ForeignKey(Shift, verbose_name='1 shift name', on_delete=models.CASCADE)
    shisetsu_id_1 = models.ForeignKey(Shisetsu, verbose_name='1 facility', on_delete=models.CASCADE)
    shift_id_2 = models.ForeignKey(Shift, verbose_name='2 shift name', on_delete=models.CASCADE)
    shisetsu_id_2 = models.ForeignKey(Shisetsu, verbose_name='2 facilities', on_delete=models.CASCADE)
    shift_id_3 = models.ForeignKey(Shift, verbose_name='3 shift name', on_delete=models.CASCADE)
    shisetsu_id_3 = models.ForeignKey(Shisetsu, verbose_name='3 facilities', on_delete=models.CASCADE)
    shift_id_4 = models.ForeignKey(Shift, verbose_name='4 shift name', on_delete=models.CASCADE)
    shisetsu_id_4 = models.ForeignKey(Shisetsu, verbose_name='4 facilities', on_delete=models.CASCADE)

I couldn't do this

2 to 3 hours to check this This is the migration.

schedule/models.py


from django.db import models
from shisetsu.models import *
from accounts.models import *

# Create your models here.

class Shift(models.Model):
    id = models.IntegerField(verbose_name='Shift ID',primary_key=True)
    name = models.CharField(verbose_name='Shift name', max_length=1)
    start_time = models.TimeField(verbose_name="Start time")
    end_time = models.TimeField(verbose_name="ending time")
    wrok_time = models.IntegerField(verbose_name='Working hours')

    def __str__(self):
        return self.name
    
class Schedule(models.Model):
    id = models.IntegerField(verbose_name='Schedule ID',primary_key=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    date = models.DateField(verbose_name='date')
    shift_name_1 = models.ForeignKey(Shift, verbose_name='1 shift name', related_name='shift_name1',on_delete=models.SET_NULL,null= True)
    shisetsu_name_1 = models.ForeignKey(Shisetsu, verbose_name='1 facility', related_name='shisetsu_name1',on_delete=models.SET_NULL,null= True)
    shift_name_2 = models.ForeignKey(Shift, verbose_name='2 shift name', related_name='shift_name2',on_delete=models.SET_NULL,null= True)
    shisetsu_name_2 = models.ForeignKey(Shisetsu, verbose_name='2 facilities', related_name='shisetsu_name2',on_delete=models.SET_NULL,null= True)
    shift_name_3 = models.ForeignKey(Shift, verbose_name='3 shift name', related_name='shift_name3',on_delete=models.SET_NULL,null= True)
    shisetsu_name_3 = models.ForeignKey(Shisetsu, verbose_name='3 facilities', related_name='shisetsu_name3',on_delete=models.SET_NULL,null= True)
    shift_name_4 = models.ForeignKey(Shift, verbose_name='4 shift name', related_name='shift_name4',on_delete=models.SET_NULL,null= True)
    shisetsu_name_4 = models.ForeignKey(Shisetsu, verbose_name='4 facilities', related_name='shisetsu_name4',on_delete=models.SET_NULL,null= True)

I finally finished it… It was long, and if you want to refer to the same table more than once in one table, you have to set each rated_name.

Changed id to auto-increment when entering tentative data

schedule/models.py


from django.db import models
from shisetsu.models import *
from accounts.models import *

# Create your models here.

class Shift(models.Model):
    id = models.AutoField(verbose_name='Shift ID',primary_key=True)
    name = models.CharField(verbose_name='Shift name', max_length=1)
    start_time = models.TimeField(verbose_name="Start time")
    end_time = models.TimeField(verbose_name="ending time")
    wrok_time = models.IntegerField(verbose_name='Working hours')

    def __str__(self):
        return self.name
    
class Schedule(models.Model):
    id = models.AutoField(verbose_name='Schedule ID',primary_key=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    date = models.DateField(verbose_name='date')
    shift_name_1 = models.ForeignKey(Shift, verbose_name='1 shift name', related_name='shift_name1',on_delete=models.SET_NULL,null= True)
    shisetsu_name_1 = models.ForeignKey(Shisetsu, verbose_name='1 facility', related_name='shisetsu_name1',on_delete=models.SET_NULL,null= True)
    shift_name_2 = models.ForeignKey(Shift, verbose_name='2 shift name', related_name='shift_name2',on_delete=models.SET_NULL,null= True)
    shisetsu_name_2 = models.ForeignKey(Shisetsu, verbose_name='2 facilities', related_name='shisetsu_name2',on_delete=models.SET_NULL,null= True)
    shift_name_3 = models.ForeignKey(Shift, verbose_name='3 shift name', related_name='shift_name3',on_delete=models.SET_NULL,null= True)
    shisetsu_name_3 = models.ForeignKey(Shisetsu, verbose_name='3 facilities', related_name='shisetsu_name3',on_delete=models.SET_NULL,null= True)
    shift_name_4 = models.ForeignKey(Shift, verbose_name='4 shift name', related_name='shift_name4',on_delete=models.SET_NULL,null= True)
    shisetsu_name_4 = models.ForeignKey(Shisetsu, verbose_name='4 facilities', related_name='shisetsu_name4',on_delete=models.SET_NULL,null= True)

As a continuation, I want to display the shift once made correctly, so It's a hassle, but I'm going to make a one-month shift by hand.

When I try to register a schedule, shift is a required input, so I released it.

schedule/models.py


from django.db import models
from shisetsu.models import *
from accounts.models import *

# Create your models here.

class Shift(models.Model):
    id = models.AutoField(verbose_name='Shift ID',primary_key=True)
    name = models.CharField(verbose_name='Shift name', max_length=1)
    start_time = models.TimeField(verbose_name="Start time")
    end_time = models.TimeField(verbose_name="ending time")
    wrok_time = models.IntegerField(verbose_name='Working hours')

    def __str__(self):
        return self.name
    
class Schedule(models.Model):
    id = models.AutoField(verbose_name='Schedule ID',primary_key=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    date = models.DateField(verbose_name='date')
    shift_name_1 = models.ForeignKey(Shift, verbose_name='1 shift name', related_name='shift_name1',on_delete=models.SET_NULL,null= True)
    shisetsu_name_1 = models.ForeignKey(Shisetsu, verbose_name='1 facility', related_name='shisetsu_name1',on_delete=models.SET_NULL,blank=True, null=True)
    shift_name_2 = models.ForeignKey(Shift, verbose_name='2 shift name', related_name='shift_name2',on_delete=models.SET_NULL,blank=True, null=True)
    shisetsu_name_2 = models.ForeignKey(Shisetsu, verbose_name='2 facilities', related_name='shisetsu_name2',on_delete=models.SET_NULL,blank=True, null=True)
    shift_name_3 = models.ForeignKey(Shift, verbose_name='3 shift name', related_name='shift_name3',on_delete=models.SET_NULL,blank=True, null=True)
    shisetsu_name_3 = models.ForeignKey(Shisetsu, verbose_name='3 facilities', related_name='shisetsu_name3',on_delete=models.SET_NULL,blank=True, null=True)
    shift_name_4 = models.ForeignKey(Shift, verbose_name='4 shift name', related_name='shift_name4',on_delete=models.SET_NULL,blank=True, null=True)
    shisetsu_name_4 = models.ForeignKey(Shisetsu, verbose_name='4 facilities', related_name='shisetsu_name4',on_delete=models.SET_NULL,blank=True, null=True)

Temporarily registered 60 shift data

image.png

image.png

From here I will create a screen that looks like the shift

Recommended Posts

Create a model for your Django schedule
Create a Django schedule
[Django] Create a model suitable for phone numbers / zip codes
Create a dashboard for Network devices with Django!
[Python] 2 Create a risk-return map for your asset portfolio
Create a homepage with django
Create a Django login screen
Create your own Django middleware
Create a social integration API for smartphone apps with Django
[Python] Create a screen for HTTP status code 403/404/500 with Django
Steps to create a Django project
[Django] Create your own 403, 404, 500 error pages
Create a file uploader with Django
Create a LINE Bot in Django
Create a python environment on your Mac
Let's create a virtual environment for Python
Commands for creating a new django project
Build a TOP screen for Django users
Get a reference model using Django Serializer
Create your first app with Django startproject
Create a Hatena dictionary for SKK (additional)
Implement a Custom User Model in Django
Write a short if-else for Django Template
Create a survival prediction model for Kaggle Titanic passengers without using Python
Django model: ManyToManyField
Create a LINE BOT with Minette for Python
Django Tutorial Summary for Beginners by Beginners (Model, Admin)
Django 1.9 for internationalization
Create a simple momentum investment model in Python
How to create a shortcut command for LINUX
Create a one-file hello world application with django
How to create a Rest Api in Django
Until you create a new app in Django
[Machine learning] Create a machine learning model by performing transfer learning with your own data set
Create a wheel of your own OpenCV module
I want to create a lunch database [EP1] Django study for the first time
I want to create a lunch database [EP1-4] Django study for the first time
Create a dataset of images to use for learning
A memo to create a virtual environment (venv) before Django
Created a fooling image for the caption generative model
Create a Todo app with Django REST Framework + Angular
I tried to create a table only with Django
Create your own Big Data in Python for validation
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
Create a QR code for the URL on Linux
Scraping your Qiita articles to create a word cloud
Create a Todo app with the Django REST framework
[Go] How to create a custom error for Sentry
Create a Todo app with Django ③ Create a task list page
Make the model a string on a Django HTML template
How to create a local repository for Linux OS
Create a Layer for AWS Lambda Python with Docker
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
Create a 3D model viewer with PyQt5 and PyQtGraph
To myself as a Django beginner (1) --Create a project app--
To myself as a Django beginner (4) --Create a memo app--
Create a Todo app with Django ⑤ Create a task editing function
xgboost: A valid machine learning model for table data
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~