Model in Django
Django ๊ธฐ๋ณธ ํ๋ฆ
url
์ ์ ์ํ๋คurls.py
๋ ์ด์ ํ๋ค!
views.py
์ ์คํํ ํจ์๋ฅผ ๋ง๋ ๋ค๋ฐํํ
html
๋ฅผ ๋ง๋ ๋ค
Django project / app ์ค์
Django๋ ํ๋์ project๊ฐ ๋ณต์์ app์ ๊ฐ์ง๋ ๊ตฌ์กฐ๋ก ๋์ด์๋ค
๊ฐ๊ฐ์ app๋ค์ MTV pattern์ ๊ฐ๊ณ ์๋ค
๋ค์ค app์ผ๋ก ๊ตฌ์ฑ๋๋ ๊ฒฝ์ฐ ์ด๋ฆ ์ค๋ณต์ด ๊ฐ๋ฅํ์ฌ
template/{app์ด๋ฆ}/{}.html
์ผ๋ก ๊ตฌ์ฑํ๋คwhy?
๊ฐ๋ณ app์ ์์ฑ๋ templates folder์ ํ์ directory๋ template file๋ก ํ์ฉ๋๋ค (default)
Django๋ template file์ ํ์ํ๋ ๊ณผ์ ์์
settings.py
์DIR
๊ณผINSTALLED_APPS
์ ์ ์ธ ์์์ ๋ฐ๋ฅด๊ธฐ ๋๋ฌธ์ ์ด๋ฆ ์ค๋ณต์ ๋ง๊ณ ์ template ๋ฐ์ app ์ด๋ฆ๊ณผ ๋์ผํ folder๋ฅผ ๋๋ค
1. Project ์์ฑ
$ django-admin startproject {ํ๋ก์ ํธ ๋ช
}
2. Project ๊ธฐ๋ณธ ์ค์ - settings.py
settings.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# app/templates/ ๊ฐ ์๋ ๋ค๋ฅธ ํด๋์ templates๋ฅผ ๋ง๋ค๊ณ ์ถ์ ๊ฒฝ์ฐ ๋ช
์ํ๋ ๊ฒ
'DIRS': [os.path.join(BASE_DIR, 'templates')], # project root ๊ฒฝ๋ก์ `templates` folder๋ฅผ ๋ง๋ค ๊ฒฝ์ฐ `templates`๋ง
# ๋ฑ๋ก๋ ์ฑ์ templates folder๋ฅผ ๋ชจ๋ ๋ค ๊ฐ์ templates ๋ก ๋ณด๊ฒ ๋ค => True
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }
3. app ์์ฑ (articles)
app์ด๋ฆ์ ์ผ๋ฐ์ ์ผ๋ก ๋ณต์ํ์ผ๋ก ๊ตฌ์ฑ๋๋ค
$ python manage.py startapp articles
app ๋ฑ๋ก
settings.py
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'articles', 'django_extensions', ]
4. urls.py
์์ฑ
urls.py
์์ฑํ๋ก์ ํธ ํด๋
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/',include('articles.urls'))
]
๊ฐ๋ณ app
from django.urls import path
from . import views
urlpatterns = [
#/articles/
path('new/', views.new),
path('create/', views.create),
path('', views.index),
]
MTV์ ์ญํ
view
url
request (์์ฒญ ๊ด๋ จ ์ ๋ณด)
return render()
Template
html
DTL
๋ฐ๋ณต / ๋ณด๊ฑด /ํํฐ
Model
DB
Model in Django
Model
: Data์ ๋ํ ๋จ์ผ ์ ๋ณด ์์ค
Migrations
: ๋ชจ๋ธ์ ๋ณ๊ฒฝ์ฌํญ๋ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง์ ๋ฐ์ํ๋ ๋ฐฉ๋ฒ
Migration ํ๋ฆ
Model ์์ฑ/์์ /์ญ์ ๋ฑ
migration
ํ์ผ ์์ฑmigration file์ model์ ๋ณ๊ฒฝ์ฌํญ์ ๊ธฐ๋กํ๊ณ , database์ ๋ฐ์ํ๊ธฐ ์ํ ์ฝ๋๋ค๋ก ๊ตฌ์ฑ๋๋ค
migration file์ database scheme๋ฅผ ์ํ
๋ฒ์ ๊ด๋ฆฌ ์์คํ
์ด๋ผ๊ณ ์๊ฐํ์! ->git
migrate
๋ฅผ ํตํ database์ ์ ์ฉ
Model
Migration
ORM (Query mtehods, QuerySet API)
MTV pattern์์ ๋ฐ์ดํฐ ๊ด๋ฆฌ
Model๋ก ์ ์๋ db schema๋ฅผ ๋ฐ์
db๋ฅผ ์กฐ์ํ๋ query๋ฌธ (python ๊ฐ์ฒด ์กฐ์์ผ๋ก ๊ฐ๋ฅํ๋ค)
Model ํ์ฉ
1. model ์ ์
models.py
from django.db import models
# Create your models here.
class Article(models.Model):
title = models.CharField(max_length=140)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
models.Model
์ ์์๋ฐ์ class๋ฅผ ์์ฑํ๋ค์์ฑ์ผ๋ก๋ ๋ด๊ฐ ๊ตฌ์ฑํ๊ณ ์ถ์ table์ column์ ์ด๋ฆ์ ์ง์ ํ๊ณ , data type์ ๋ง์ถฐ์ field๋ฅผ ์ ์ํ๋ค
id ํ๋๋ ์๋์ ์ผ๋ก pk ๊ฐ์ผ๋ก ์์ฑ๋๋ค.
์์์ ์ ์๋ ํ๋์ ์ต์ ์ ๋ณด๋ ๋ค์๊ณผ ๊ฐ๋ค.
CharField
:max_length
: ํ์
DateTimeField
auto_now_add
: (์ ํ) ์์ฑ์์๋ง ์๋์ผ๋ก ํด๋น ์๊ฐ ๊ฐ ์ค์ auto_now
: (์ ํ) ์์ ์๋ง๋ค ์๋์ผ๋ก ํด๋น ์๊ฐ ๊ฐ ์ค์
์ด์ธ์ ํ๋๋ https://docs.djangoproject.com/ko/2.1/ref/models/fields/#field-types ๋งํฌ์์ ํ์ธ!
CharField
vs TextField()
CharField
vs TextField()
: ์ค์ ๋ก <form>
tag๋ก data๋ฅผ ๋ฐ์๋ <input>
์ผ๋ก ๋ฐ์ ์ง <textarea>
๋ก ๋ฐ์์ง์ ๋ฐ๋ผ ์ ํํ๊ธฐ
2. migration
Migrations are Djangoโs way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. ๋ง์ด๊ทธ๋ ์ด์ ์ django์์ ๋ชจ๋ธ์ ๋ณ๊ฒฝ ์ฌํญ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง์ ๋ฐ์ํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ด๋ค.
2-1. makemigrations
$ python manage.py makemigrations
Migrations for 'articles':
articles/migrations/0001_initial.py
- Create model Article
์ ์๋ model์ database์ ๋ฐ์ํ๊ธฐ ์ํด์๋ migration ๋ช ๋ น์ด๋ฅผ ํตํด migration file์ ์์ฑํ๋ค
๋ง์ด๊ทธ๋ ์ด์ ํ์ผ์ ๋ชจ๋ธ์ ๋ณ๊ฒฝ์ฌํญ์ ๊ธฐ๋กํ๋ฉฐ, app ๋ณ๋ก ์๋ migrations/ ํด๋์ ๊ธฐ๋ก๋๋ค. ์ต์ด์ 0001_initial.py ๋ผ๋ ํ์ผ์ด ์์ฑ๋์ด ์์ ๊ฒ์ด๋ค.
migration file์ model์ ๋ณ๊ฒฝ์ฌํญ์ ๊ด๋ฆฌํ๋ค
Modeling ํ ๋ด์ฉ์ db์ ๋ฐ์ ํ ์ค๋น๋ฅผ ํ๋ ๊ฒ!
2-2. migrate
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, articles, auth, 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 articles.0001_initial... 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 sessions.0001_initial... OK
์์ฑ๋ migration file์ database์ ๋ฐ์ํ๊ธฐ ์ํ ๋ช ๋ น์ด
์์ ๊ฐ์ด ๋ง์ ๋ณด์ด๋ ๊ฒ์ django๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ํ์ฉํ๊ณ ์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ง์ด๊ทธ๋ ์ด์ ํ์ผ๊น์ง ๋ฐ์๋์๊ธฐ ๋๋ฌธ์ด๋ค
์์ผ๋ก๋ ํ๋ก์ ํธ ์์ฑ๊ณผ ๋์์
python manage.py migrate
๋ฅผ ํ์!
Migration Flow
๋ง์ด๊ทธ๋ ์ด์ ์์ฑ
$ python manage.py makemigrations
๋ง์ด๊ทธ๋ ์ด์ DB ๋ฐ์ ์ฌ๋ถ ํ์ธ
$ python manage.py showmigratons
๋ง์ด๊ทธ๋ ์ด์ ์ ๋์๋๋ SQL๋ฌธ ์ถ๋ ฅ
$ python manage.py sqlmigrate app_label migration_name
๋ง์ด๊ทธ๋ ์ด์ ํ์ผ์ ๋ด์ฉ์ DB์ ์ต์ข ๋ฐ์
$ python manage.py migrate
+
admin ๋ฑ๋ก
# django_crud/articles/admin.py
from django.contrib import admin
# Register your models here.
from .models import Article
admin.site.register(Article)
Django ORM
๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์กฐ์์ CRUD(Create, Read, Update, Delete) operation ์ด๋ผ๊ณ ํ๋ค.
ORM (Object Relational Mapping)
DB์ OOP language ๊ฐ์ ํธํ๋์ง ์๋ data๋ฅผ ๋ณํํ๋ programming ๊ธฐ๋ฒ
ORM์ db์ ์ ์ฅ๋์ด ์๋ ๊ฐ์ object๋ก mapping ํด์ค๋ค!
Python ๊ฐ์ฒด ์กฐ์(method ํธ์ถ)์ผ๋ก db๋ฅผ ์กฐ์ํ๋ ๊ฒ!
Django shell
python interactive interpreter๋ฅผ django ํ๋ก์ ํธ์ ๋ง๊ฒ ์ธ ์ ์๋ ๊ธฐ๋ฅ
$ python manage.py shell
์ถ๊ฐ์ ์ธ ํจํค์ง ์ค์น๋ฅผ ํตํด ํธํ๊ฒ ํ์ฉํ ์ ์๋ค.
$ pip install django-extensions ipython
django-extensions ๋ django ๊ฐ๋ฐ์ ์์ด์ ์ ์ฉํ ๊ธฐ๋ฅ๋ค์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ค.
ipython
์ ์ธํฐ๋ ํฐ๋ธ ์์ ์กฐ๊ธ ๋ ํธํ๊ฒ ํ์ฉํ๊ธฐ ์ํด์ ์ค์น
์ค์น ์ดํ์, settings.py ์ ๋ค์์ ๋ด์ฉ์ ์ถ๊ฐํ๋ค. (์ฝค๋ง ์ ์)
# django_crud/settings.py INSTALLED_APPS = [ ... 'django_extensions', 'articles', ]
๊ทธ๋ฆฌ๊ณ ์ด์ ๋ถํฐ๋ ์๋์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ๋ค.
$ python manage.py shell_plus
1. ์์ฑ
article = Article()
article.title = '์ ๋ชฉ'
article.content = '๋ด์ฉ'
article.save()
2. ์กฐํ
์ ์ฒด ๋ฐ์ดํฐ ์กฐํ
Article.objects.all() >> <QuerySet [<Article: Article object (1)>]>
๋จ์ผ ๋ฐ์ดํฐ ์กฐํ
๋จ์ผ ๋ฐ์ดํฐ ์กฐํ๋ ๊ณ ์ ํ ๊ฐ์ธ id๋ฅผ ํตํด ๊ฐ๋ฅํ๋ค.
Article.objects.get(id=1) >> <Article: Article object (1)>
In [2]: Article.objects.get(id=3) Out[2]: Title: sample title & Content: sample content In [3]: Article.objects.get(pk=3) Out[3]: Title: sample title & Content: sample content
id == pk!
์ค๋ณต์ ๋ถํํ๋ค
3. ์์
a1 = Article.objects.get(id=1)
a1.title = '์ ๋ชฉ ์์ '
a1.save()
์์ ์ด ๋์๋์ง ํ์ธํ๊ธฐ ์ํด์ ๋ฐ์ดํฐ ์กฐํ๋ฅผ ๋ค์ ํด๋ณด์
4. ์ญ์
a1 = Article.objects.get(id=1)
a1.delete()
>> (1, {'articles.Article': 1})
Admin ํ์ด์ง ํ์ฉ
1. ๊ด๋ฆฌ์ ๊ณ์ ์์ฑ
$ python manage.py createsuperuser
์ฌ์ฉ์ ์ด๋ฆ (leave blank to use 'ubuntu'): admin1
์ด๋ฉ์ผ ์ฃผ์:
Password:
Password (again):
Superuser created successfully.
2. admin ๋ฑ๋ก
admin ํ์ด์ง๋ฅผ ํ์ฉํ๊ธฐ ์ํด์๋ app ๋ณ๋ก ์๋ admin.py์ ์ ์ ๋์ด์ผ ํ๋ค
# django_crud/articles/admin.py
from django.contrib import admin
# Register your models here.
from .models import Article
admin.site.register(Article)
3. ํ์ธ
/admin/ url๋ก ์ ์ํ์ฌ, ๊ด๋ฆฌ์ ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธ
+
Tips
$ python manage.py sqlmigrate articles 0001
BEGIN;
--
-- Create model Article
--
CREATE TABLE "articles_article" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(20) NOT NULL, "content" text NOT NULL);
COMMIT;
Fat Model
MVC
M (C) V
MTV
M (V) T
-> Make model fat!!!!
Last updated
Was this helpful?