ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [django] DB 생성과 마이그레이션
    Programing/Python 2019. 4. 9. 07:27

    장고 앱에서는 MVT(Model/View/Template) 구조를 가지고 있는데요.

    데이터 생성 및 관리를 Model 에서는 database를 사용하고 있습니다.

    sqlite를 default database로 사용하고 있는데 확장성을 위해서 다른 데이터베이스도 사용이 가능합니다.

    이 글에서는 프로젝트 내에 앱과 DB 생성 과정을 정리해 보았습니다.

    1. app 생성

    $> python manage.py startapp elections

    2. app 생성 확인

    $> tree

    ├── elections
    │   ├── admin.py
    │   ├── apps.py
    │   ├── __init__.py
    │   ├── migrations
    │   │   ├── __init__.py
    │   │   └── __pycache__
    │   │       └── __init__.cpython-36.pyc
    │   ├── models.py
    │   ├── __pycache__
    │   │   ├── admin.cpython-36.pyc
    │   │   ├── __init__.cpython-36.pyc
    │   │   └── models.cpython-36.pyc
    │   ├── tests.py
    │   └── views.py
    ├── manage.py

    3. settings.py 에 elections app 추가

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    INSTALLED_APPS = [
        'polls.apps.PollsConfig',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'elections',
    ]

    4. model.py 수정

    1
    2
    3
    4
    5
    6
    7
    from django.db import models
    # Create your models here.
    class Candidate(models.Model):
        name = models.CharField(max_length=10)
        introduction = models.TextField() # no limit of leng
        area = models.CharField(max_length=15)
        party_number = models.IntegerField(default=1)

    5. makemigrations :0001_initial.py 생성

    $> python manage.py makemigrations

    Migrations for 'elections':
      elections/migrations/0001_initial.py
        - Create model Candidate

    6. initial.py 파일 생성 확인

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    class Migration(migrations.Migration):
     
        initial = True
     
        dependencies = [
        ]
     
        operations = [
            migrations.CreateModel(
                name='Candidate',
                fields=[
                    ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                    ('name', models.CharField(max_length=10)),
                    ('introduction', models.TextField()),
                    ('area', models.CharField(max_length=15)),
                    ('party_number', models.IntegerField(default=1)),
                ],
            ),
        ]

    7. 실제 DB 파일 생성

    $> python manage.py migrate

    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, elections, polls, sessions
    Running migrations:
      Applying elections.0001_initial... OK

    8. DB 확인

    $> sqlite3 db.sqlite3 
    SQLite version 3.22.0 2018-01-22 18:45:57
    Enter ".help" for usage hints.
    sqlite> .tables
    auth_group                  django_content_type       
    auth_group_permissions      django_migrations         
    auth_permission             django_session            
    auth_user                   elections_candidate       
    auth_user_groups            polls_choice              
    auth_user_user_permissions  polls_question            
    django_admin_log

    다음과 같이 DB 테이블이 생성된 것을 확인할 수 있습니다.

    ※ 위 내용은 programmers.co.kr 온라인 강의를 수강한 후에 작성되었습니다.

    댓글

Designed by Tistory.