👉 예시
group_name,displayed_price,discount_rate,company,sub_category_id,delivery_id
캐더린 통수납 침대,599000,68,리샘가구,1,1
다락 빅 수납침대,650000,63,포덕홈,1,1
헤르만 무헤드 통수납 침대,309000,69,리샘가구,1,1
단잠슬림가드 저상형 패밀리 침대,798000,79,모던바로크,1,1
산타페 하이브리드 침대,189000,20,지누스,1,1
레아 사계절 고무나무 원목침대,399000,53,퍼니코,1,1
👉 import & 환경변수 설정
import os, django, csv, sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "homes.settings")
django.setup()
👉 CSV 파일 경로 설정 & 모델 import
from products.models import *
CSV_PATH_MENU = 'csv/Menu.csv'
CSV_PATH_CATEGORY = 'csv/Category.csv'
CSV_PATH_SUBCATEGORY = 'csv/SubCategory.csv'
CSV_PATH_PRODUCTGROUP = 'csv/ProductGroup.csv'
CSV_PATH_PRODUCT = 'csv/Product.csv'
CSV_PATH_COLOR = 'csv/Color.csv'
CSV_PATH_PRODUCTIMAGE = 'csv/ProductImage.csv'
CSV_PATH_REVIEW = 'csv/Review.csv'
CSV_PATH_DELIVERY = 'csv/Delivery.csv'
CSV_PATH_PRODUCTCOLOR = 'csv/ProductColor.csv'
👉 작성한 db_uploader.py
import os, django, csv, sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "homes.settings")
django.setup()
from products.models import *
CSV_PATH_MENU = 'csv/Menu.csv'
CSV_PATH_CATEGORY = 'csv/Category.csv'
CSV_PATH_SUBCATEGORY = 'csv/SubCategory.csv'
CSV_PATH_PRODUCTGROUP = 'csv/ProductGroup.csv'
CSV_PATH_PRODUCT = 'csv/Product.csv'
CSV_PATH_COLOR = 'csv/Color.csv'
CSV_PATH_PRODUCTIMAGE = 'csv/ProductImage.csv'
CSV_PATH_REVIEW = 'csv/Review.csv'
CSV_PATH_DELIVERY = 'csv/Delivery.csv'
CSV_PATH_PRODUCTCOLOR = 'csv/ProductColor.csv'
with open(CSV_PATH_MENU) as in_file:
data_reader = csv.reader(in_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
name = row[0]
image_url = row[1]
Menu.objects.create(name = name, image_url = image_url)
with open(CSV_PATH_CATEGORY) as in_file:
data_reader = csv.reader(in_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
name = row[0]
menu_id = row[1]
Category.objects.create(name = name, menu_id = menu_id)
with open(CSV_PATH_SUBCATEGORY) as in_file:
data_reader = csv.reader(in_file)
next(data_reader, None)
for row in data_reader:
if row[0]:
name = row[0]
category_id = row[1]
SubCategory.objects.create(name = name, category_id = category_id)
with open(CSV_PATH_DELIVERY) as in_file:
data_reader = csv.reader(in_file)
next(data_reader, None)
for row in data_reader:
delivery_type = row[0]
payment_type = row[1]
delivery_fee = row[2]
Delivery.objects.create(delivery_type = delivery_type, payment_type = payment_type, delivery_fee = delivery_fee)
with open(CSV_PATH_PRODUCTGROUP) as in_file:
data_reader = csv.reader(in_file)
next(data_reader, None)
for row in data_reader:
name = row[0]
company = row[3]
displayed_price = row[1]
discount_rate = row[2]
sub_category = row[4]
delivery = row[5]
ProductGroup.objects.create(name = name, company = company, displayed_price = displayed_price, discount_rate = discount_rate, sub_category_id = sub_category, delivery_id = delivery)
with open(CSV_PATH_PRODUCT) as in_file:
data_reader = csv.reader(in_file)
next(data_reader, None)
for row in data_reader:
name = row[0]
price = row[1]
product_group = row[2]
Product.objects.create(name = name, price = price, product_group_id = product_group)
with open(CSV_PATH_COLOR) as in_file:
data_reader = csv.reader(in_file)
next(data_reader, None)
for row in data_reader:
name = row[0]
Color.objects.create(name = name)
with open(CSV_PATH_PRODUCTIMAGE) as in_file:
data_reader = csv.reader(in_file)
next(data_reader, None)
for row in data_reader:
image_url = row[0]
product_group = row[1]
ProductImage.objects.create(image_url = image_url, product_group_id = product_group)
with open(CSV_PATH_REVIEW) as in_file:
data_reader = csv.reader(in_file)
next(data_reader, None)
for row in data_reader:
user = row[0]
content = row[1]
star_rate = row[2]
product_group = row[3]
Review.objects.create(user_id = user, content = content, star_rate = star_rate, product_group_id = product_group)
with open(CSV_PATH_PRODUCTCOLOR) as in_file:
data_reader = csv.reader(in_file)
next(data_reader, None)
for row in data_reader:
product = row[0]
color = row[1]
ProductColor.objects.create(product_id = product, color_id = color)