January 14, 2021 ← Last time: Display template in Day 8 Template View
This article is not a single article. I wrote it as a diary, so I think it will be useful for those who are new to it. If you want to learn Django, we recommend reading it from [Day 1] Django Development Environment.
The theme this time is "Creating a model". So far, it's very rudimentary, but we've looked at View and Template. The rest is Model. Django has a mechanism that allows you to link with a database and perform validation processing smartly by giving various information to the model. This time, let's make a simple model and learn because it is a bulletin board.
First, create a thread application. This is an application that handles things related to bulletin board threads.
(venv)$ ./manage.py startapp thread
Add thread/urls.py, add application to mysite/settings.py, and add URL to mysite/urls.py as well as base application. This area is similar to the base application. [Day 4] Application Creation For reference
mysite/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.sitemaps',
'debug_toolbar',
'base',
+ 'thread',
]
mysite/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('base.urls')),
+ path('thread/', include('thread.urls')),
]
thread/urls.py
from django.urls import path
from . import views
app_name = 'thread'
urlpatterns = [
]
Now the additional processing of the application is OK.
The thread application model is assumed to be as follows. First, consider the following model so that it has the minimum functions of the bulletin board.
thread/models.py
from django.db import models
class TopicManager(models.Manager):
#Added processing related to Topic operation
pass
class CommentManager(models.Manager):
#Added processing related to Comment operation
pass
class CategoryManager(models.Manager):
#Added processing related to Category operation
pass
class Category(models.Model):
name = models.CharField(
'Category name',
max_length=50,
)
url_code = models.CharField(
'URL code',
max_length=50,
null=True,
blank=False,
unique=True,
)
sort=models.IntegerField(
verbose_name='sort',
default=0,
)
objects = CategoryManager
def __str__(self):
return self.name
class Topic(models.Model):
user_name = models.CharField(
'name',
max_length=30,
null=True,
blank=False,
)
title = models.CharField(
'title',
max_length=255,
null = False,
blank = False,
)
message = models.TextField(
verbose_name='Text',
null=True,
blank=False,
)
category = models.ForeignKey(
Category,
verbose_name='Category',
on_delete=models.PROTECT,
null=True,
blank=False,
)
created = models.DateTimeField(
auto_now_add=True,
)
modified = models.DateTimeField(
auto_now=True,
)
objects = TopicManager()
def __str__(self):
return self.title
class Comment(models.Model):
id = models.BigAutoField(
primary_key=True,
)
no = models.IntegerField(
default=0,
)
user_name = models.CharField(
'name',
max_length=30,
null=True,
blank=False,
)
topic = models.ForeignKey(
Topic,
on_delete=models.PROTECT,
)
message = models.TextField(
verbose_name='Posted content'
)
pub_flg = models.BooleanField(
default=True,
)
created = models.DateTimeField(
auto_now_add=True,
)
objects = CommentManager()
def __str__(self):
return '{}-{}'.format(self.topic.id, self.no)
From here, we will enter the above (thread/models.py) explanation. Therefore, I will post the URL of the head family. Honke After that, I will put the explanation below so that you do not have to jump to the link, but since it is exactly the same as the head family, it is recommended to fly to the head family and refer to it.
models.CASCADE: Models referenced along with foreign key models are also deleted models.PROTECT: Protected (cannot be deleted if referenced) models.SET_NULL: ID will be NULL if the foreign key model is deleted models.SET_DEFAULT: The value set by default is set. Is PROTECT recommended? CASCADE is a little risky, but I don't think it's a problem depending on the design.
See you again
← Last time: Display template in Day 8 Template View → Next time: Day 10 Database Migration
Recommended Posts