django study 02 - class 이용해서 db 생성하기

db 생성을 위한 class 생성하기

lms/licenses/models.py

from django.db import models

class license_list(models.Model):
    account = models.CharField(max_length=255)
    end_user = models.CharField(max_length=255)
    product = models.CharField(max_length=255)
    product_key = models.CharField(max_length=255)
    expiration_date = models.CharField(max_length=255)
    volumemargin = models.CharField(max_length=255)
    margin = models.CharField(max_length=255)
    activation_remain = models.CharField(max_length=255)
    activation_total = models.CharField(max_length=255)

class user_list(models.Model):


apps.py에 저장된 licenses app의 config를 django에 등록하기


lms/licenses/apps.py

from django.apps import AppConfig

class LicensesConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'licenses'

lms/lms/settings.py


from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = 'django-insecure-nf*9hxfrzo@7r5kw9jbrh($6)$io&gp0rbmv8_c7x9*-0&z$s-'

DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'licenses.apps.LicensesConfig'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'lms.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'lms.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

STATIC_URL = 'static/'

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


class 내용을 database로 변환하기(migration)


lms/

$ python3 manage.py makemigrations
licenses/migrations/0001_initial.py
    - Create model license_list
    - Create model user_list

$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, licenses, 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 licenses.0001_initial... OK
  Applying sessions.0001_initial... OK


model 수정하기


/lms/licenses/models.py

from pyexpat import model
from django.db import models
from django.forms import IntegerField
from django.utils import timezone

class license_list(models.Model):
    account = models.CharField(max_length=255)
    end_user = models.CharField(max_length=255)
    product = models.CharField(max_length=255)
    product_key = models.CharField(max_length=255)
    expiration_date = models.DateField()
    volumemargin = models.IntegerField()
    margin = models.IntegerField()
    activation_remain = models.IntegerField()
    activation_total = models.IntegerField()
    created = models.DateTimeField(default=timezone.now)

class user_list(models.Model):
    account = models.ForeignKey(license_list, on_delete=models.CASCADE)
    end_user = models.CharField(max_length=255)

/lms

$ python3 manage.py makemigrations
licenses/migrations/0002_license_list_created.py
    - Add field created to license_list

$ python3 manage.py migrate
perations to perform:
  Apply all migrations: admin, auth, contenttypes, licenses, sessions
Running migrations:
  Applying licenses.0002_license_list_created... OK


django class 가 생성하는 sql 문 확인하기


/lms

$ python3 manage.py sqlmigrate licenses 0001
BEGIN;
--
-- Create model license_list
--
CREATE TABLE "licenses_license_list" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "account" varchar(255) NOT NULL, "end_user" varchar(255) NOT NULL, "product" varchar(255) NOT NULL, "product_key" varchar(255) NOT NULL, "expiration_date" date NOT NULL, "volumemargin" integer NOT NULL, "margin" integer NOT NULL, "activation_remain" integer NOT NULL, "activation_total" integer NOT NULL);
--
-- Create model user_list
--
CREATE TABLE "licenses_user_list" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "end_user" varchar(255) NOT NULL, "account_id" bigint NOT NULL REFERENCES "licenses_license_list" ("id") DEFERRABLE INITIALLY DEFERRED);
CREATE INDEX "licenses_user_list_account_id_d6fde2b6" ON "licenses_user_list" ("account_id");
COMMIT;

댓글 쓰기

0 댓글