1차 프로젝트 🩺🍪 Dr.Tart - csv uploader

Hailey Park·2021년 12월 5일
1

1차 프로젝트

목록 보기
3/6
post-thumbnail

다량의 데이터를 한번에 DB에 넣기 위해서 csv uploader 파이썬 파일을 만들어서 업로드한다.
이 과정에서 나와 팀원들을 미치게 하던 요상한 에러가 있었다.

엑셀파일을 csv로 변환하는 과정에서 생긴 것 같은데, 알 수 없는 빨간 네모가 생겨서 이 알 수 없는 빨간 네모 때문에 몇 시간을 에러와 씨름했다. 결국 이 빨간 네모를 지우고나서 업로드에 성공했음.

팀원의 컴퓨터에서는 저 빨간네모가 심지어 보이지도 않아서, 파이썬 shell에 print(row)를 해보고 나서 김상훈님 앞에 o8x 였나? 이렇게 생긴 이상한 문자들을 보고 허공에 백스페이스를 누른 후에 실행을 했더니 업로드가 완료되었다(?)...

orders.csv

address,order_status,user
서울시 영등포구 신길동,paid,유민혁
인천광역시 어쩌구,pending,길도연
부산광역시 어쩌구,preparing,성주호
경기도 고양시 일산동구 어쩌구,shipped,홍유진
서울시 강남구 테헤란로 427,delivered,김상훈

db_uploader.py

import os
import django
import csv 

from pathlib import Path

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

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dr_tart.settings")
django.setup()

from orders.models     import Cart, OrderItem, Order, OrderStatus
from products.models   import Menu, Product, Category, Image, ReviewComment, Review, Menu
from users.models      import User

CSV_PATH_USERS = './csv/users.csv'

with open(CSV_PATH_USERS) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)
    for row in data_reader:
        User.objects.create(
            name = row[0], 
            email = row[1], 
            password = row[2], 
            address = row[3],
            vegan_or_not = row[4]
            )

CSV_PATH_PRODUCTS = "./csv/products.csv"

with open(CSV_PATH_PRODUCTS) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)
    for row in data_reader: 
        if not Menu.objects.filter(name=row[8]).exists():
            Menu.objects.create(
				name = row[8]
		)

with open(CSV_PATH_PRODUCTS) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)
    for row in data_reader: 
        if not Category.objects.filter(name=row[7]).exists():
            Category.objects.create(
				name = row[7],
                menu = Menu.objects.get(name=row[8])
		)
            

with open(CSV_PATH_PRODUCTS) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)
    for row in data_reader:
        if not Product.objects.filter(korean_name=row[0]).exists():  		
            Product.objects.create(
                korean_name = row[0],
                english_name = row[1],
                thumbnail_image_url = row[2],
                price = row[3],
                vegan_or_not = row[4],
                sugar_level = row[5],
                description = row[6],
                category  = Category.objects.get(name = row[7])
		)
  
CSV_PATH_PRODUCTS = "./csv/carts.csv"

with open(CSV_PATH_PRODUCTS) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)
    for row in data_reader:
        Cart.objects.create(
			quantity = row[0],
            product = Product.objects.get(korean_name=row[1]),
            user = User.objects.get(name=row[2])
        )         
            
CSV_PATH_PRODUCTS = "./csv/images.csv"

with open(CSV_PATH_PRODUCTS) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)
    for row in data_reader:
        Image.objects.create(
			url = row[0],
            product = Product.objects.get(korean_name=row[1])
        )
        
CSV_PATH_PRODUCTS = "./csv/orders.csv"
 
with open(CSV_PATH_PRODUCTS) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)
    for row in data_reader:
        if not OrderStatus.objects.filter(status=row[1]).exists():  
            OrderStatus.objects.create(
                status = row[1],
        )  
        
with open(CSV_PATH_PRODUCTS) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)
    for row in data_reader:
        Order.objects.create(
			address = row[0],
            order_status = OrderStatus.objects.get(status = row[1]),
            user = User.objects.get(name=row[2])
        )        
        
CSV_PATH_PRODUCTS = "./csv/order_items.csv"

with open(CSV_PATH_PRODUCTS) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)
    for row in data_reader:
        OrderItem.objects.create(
			quantity = row[0],
            order = Order.objects.get(id=row[1]),
            product = Product.objects.get(korean_name=row[2]),
        )


CSV_PATH_PRODUCTS = "./csv/reviews.csv"

with open(CSV_PATH_PRODUCTS) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)
    for row in data_reader:
        Review.objects.create(
			content = row[0],
            product = Product.objects.get(korean_name=row[1]),
            user = User.objects.get(name=row[2]),
            review_image_url = row[3]
        )
  
CSV_PATH_PRODUCTS = "./csv/review_comments.csv"

with open(CSV_PATH_PRODUCTS) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)
    for row in data_reader:
        ReviewComment.objects.create(
			content = row[0],
            review = Review.objects.get(id=row[1]),
            user = User.objects.get(name=row[2])
        )
        
profile
I'm a deeply superficial person.

0개의 댓글