Django has an ORM (Object-Relational Mapping). ORM refers to the ability to convert between program source code and database data. In the case of Django, describe it in modele.py with Pythn.
Now, let's write the Post model.
/crud/blog/models.py
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
class Post(models.Model):
#Title CHAR up to 100 characters
title = models.CharField(max_length=100)
#Content text
content = models.TextField()
#Author foreign key constraint(One-to-many relation)Delete child data as well as user parent data
author = models.ForeignKey(User, on_delete=models.CASCADE)
#Post date Date type Current time
date_posted = models.DateTimeField(default=timezone.now)
#Display setting of management screen Display title
def __str__(self):
return self.title
(crud-_w5mSGH2) C:\django\crud>python manage.py makemigrations
Migrations for 'blog':
blog\migrations\0001_initial.py
- Create model Post
(crud-_w5mSGH2) C:\django\crud>
The following files are automatically generated. You don't have to edit it.
/crud/blog/migrations/0001_initial.py
# Generated by Django 3.1.1 on 2020-10-12 12:32
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('content', models.TextField()),
('date_posted', models.DateTimeField(default=django.utils.timezone.now)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
(crud-_w5mSGH2) C:\django\crud>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying blog.0001_initial... OK
Applying sessions.0001_initial... OK
(crud-_w5mSGH2) C:\django\crud>
If it is an original blog, I will post an article from the posting screen, but I have not implemented it yet. Django has an admin screen from which you can register your data. Therefore, I would like to register the article data from the management screen once.
You have to tell Django what you want to see on the admin screen. Let's modify the following files.
crud/blog/admin.py
from django.contrib import admin
from .models import Post
admin.site.register(Post)
Create an admin user to log in to the admin screen. User: admin Password: pass
(crud-_w5mSGH2) C:\django\crud>python manage.py createsuperuser
username(leave blank to use 'wmgoz'): admin
mail address: ***@***.com #← Please use your own email address
Password:
Password (again):
This password is too short. At least 8 characters are required.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
(crud-_w5mSGH2) C:\django\crud>
Let's start the development server and log in to the management screen.
python manage.py runserver
Next, let's access the management screen "http://127.0.0.1:8000/admin/". The following management screen was displayed.
Click "+ Add" next to "Posts" on the management screen to proceed to data entry.
Registration of data ①:
Registration of data ②:
Two articles have been posted.
That's all for today. Thank you very much.