데이터베이스 & CSV
회고록
아직 우리는 공통된 데이터베이스를 가지고 있지 않다. 그렇다보니 서로 같은 데이터를 이용하기 위해서 CSV파일을 이용해서 데이터를 담는 과정이 필요하다. 이 파일을 다른 팀원에게 넘겨주어서 같은 다른 팀원도 같은 데이터를 사용하게 된다.
하나의 데이터베이스를 이용하기 위해서는 AWS를 이용해야 되는데 아직 실력이 되지 않아 좀 더 시간이 걸릴거 같다.
CSV파일
upload.py
첫번째 줄에 해당하는 목록을 나타내는 이름을 제외한 데이터를 자동으로 담는다. 중요한 것은 참조하는 데이터가 있는 경우라면 반드시 먼저 불러져야한다.
import os
import sys
import csv
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "terarosa.settings")
django.setup()
from products.models import *
CATEGORY_PATH = 'csv_file/1_Category.csv'
SUBCATEGORY_PATH = 'csv_file/2_SubCategory.csv'
PRODUCT_PATH = 'csv_file/3_Product.csv'
PRODUCTIMAGE = 'csv_file/4_Productimage.csv'
SIZE = 'csv_file/5_Size.csv'
TASTE = 'csv_file/6_Taste.csv'
GRAINDING = 'csv_file/8_Grainding.csv'
TASTEBYPRODUCT = 'csv_file/7_TasteByProduct.csv'
GRAINDINGBYPRODUCT = 'csv_file/9_GraindByProduct.csv'
def insert_Category():
with open(CATEGORY_PATH) as csv_file:
data_reader = csv.reader(csv_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
name = row[0]
print(name)
Category.objects.create(name = name)
print('MENU DATA UPLOADED SUCCESSFULY!')
# insert_Category()
def insert_Subcategory():
with open(SUBCATEGORY_PATH) as csv_file:
data_reader = csv.reader(csv_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
name = row[0]
category = Category.objects.get(id = row[1])
print(name, category)
SubCategory.objects.create(name = name, category = category)
print('CATEGORY DATA UPLOADED SUCCESSFULY!')
# insert_Subcategory()
def insert_Product():
with open(PRODUCT_PATH) as csv_file:
data_reader = csv.reader(csv_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
subcategory = SubCategory.objects.get(id = row[0])
name = row[1]
eng_name = row[2]
roasting_date = row[3]
price = row[4]
Product.objects.create(
subcategory = subcategory,
name = name,
eng_name = eng_name,
price = price,
roasting_date = roasting_date,
)
print('PRODUCT DATA UPLOADED SUCCESSFULY!')
# insert_Product()
def insert_Image():
with open(PRODUCTIMAGE) as csv_file:
data_reader = csv.reader(csv_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
image_url = row[0]
product = Product.objects.get(id = row[1])
print(image_url, product)
ProductImage.objects.create(url = image_url, product = product)
print('IMAGE DATA UPLOADED SUCCESSFULY!')
# insert_Image()
# size 데이터모델링 이름 변경했다. name ==> size
def insert_Size():
with open(SIZE) as csv_file:
data_reader = csv.reader(csv_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
product = Product.objects.get(id=row[0])
name = row[1]
price = row[2]
print(product, name, price)
Size.objects.create(name = name, product = product, price = price)
print('insert_Size DATA UPLOADED SUCCESSFULY!')
# insert_Size()
def insert_Taste():
with open(TASTE) as csv_file:
data_reader = csv.reader(csv_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
name = row[0]
print(name)
Taste.objects.create(name=name)
print('TASTINGNOTE DATA UPLOADED SUCCESSFULY!')
# insert_Taste()
def insert_Grainding():
with open(GRAINDING) as csv_file:
data_reader = csv.reader(csv_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
name = row[0]
print(name)
Grainding.objects.create(type=name)
print('insert_Grainding DATA UPLOADED SUCCESSFULY!')
# insert_Grainding()
def insert_TasteByProduct():
with open(TASTEBYPRODUCT) as csv_file:
data_reader = csv.reader(csv_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
product = Product.objects.get(id=row[0])
taste = Taste.objects.get(id=row[1])
print(product, taste)
TasteByProduct.objects.create(product=product, taste=taste)
print('insert_TasteByProduct DATA UPLOADED SUCCESSFULY!')
# insert_TasteByProduct()
def insert_GraindByProduct():
with open(GRAINDINGBYPRODUCT) as csv_file:
data_reader = csv.reader(csv_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
product = Product.objects.get(id=row[0])
grainding = Grainding.objects.get(id=row[1])
print(product, grainding)
GraindByProduct.objects.create(product=product, grainding=grainding)
print('insert_GraindByProduct DATA UPLOADED SUCCESSFULY!')
# insert_GraindByProduct()