[ChickIN] -2 <csv파일로 데이터 넣기>

김가람휘·2022년 3월 4일
0

ChickIN

목록 보기
2/7

임시의 데이터들을 csv파일로 생성하여 db_uploader.py을 이용해서 DB에 팀원들 모두 동일한 데이터를 넣으면 똑같은 DB를 사용하는 효과를 볼 수 있다.

  • db에 넣을 다음과 같은 csv파일들을 작성했다.
# purchase.csv
name,
닭,
오리,
# main_categories.csv
Name,purchase_id
구이용,1
요리용,1
덕인,2
# sub_categories.csv
name,main_category_id
다리,1
윙,1
봉,1
닭가슴살,1
특수부위,1
닭요리용,2
오리요리용,3
# products.csv
name,price,description,thumbnail,sub_category_id
닭다리 1kg,8000,부드러운 닭다리살!,https://user-images.githubusercontent.com/57164712/156378709-63490ba4-bd36-45f5-acae-86462883f7ce.jpg,1
닭윙 1kg,6000,쫄깃한 닭윙!,https://user-images.githubusercontent.com/57164712/156378902-2954b6bc-ded0-42b1-b4b7-dd197a35cc0a.jpg,2
닭봉 1kg,5900,맛있는 닭봉!,https://user-images.githubusercontent.com/57164712/156378961-2e51810d-5fbd-4d11-b105-afb935a0440b.jpg,3
닭가슴살 1kg,8100,건강한 닭가슴살!,https://user-images.githubusercontent.com/57164712/156378996-7b2658c4-1670-44a0-b1cf-a1300066b8f9.jpg,4
닭발 1kg,7500,매력있는 닭발!,https://user-images.githubusercontent.com/57164712/156379038-1afb42aa-0752-4ed1-90ed-e3bd4247a2e3.png,5
닭볶음탕용 닭,6100,맛있는 닭볶음탕!,https://user-images.githubusercontent.com/57164712/156379062-c0326d1b-4611-4c8d-8905-73b855a661a5.jpg,6
삼계탕용 닭,4900,건강에 좋고 맛도 좋은 삼계탕!,https://user-images.githubusercontent.com/57164712/156379087-4ec67c57-d266-435c-aa69-82fcfb1560ac.jpg,6
백숙용 닭,11000,보양식 백숙!,https://user-images.githubusercontent.com/57164712/156379117-3ba227eb-2d7d-4be7-b5dd-388c29b3299f.jpg,6
훈제오리 200g,3600,맛있는 훈제오리!,https://user-images.githubusercontent.com/57164712/156379142-49f722cf-893c-40cf-b59b-fd5d7c29dc66.jpg,7
# product_images.csv
image_url,product_id
https://user-images.githubusercontent.com/57164712/156378709-63490ba4-bd36-45f5-acae-86462883f7ce.jpg,1
https://user-images.githubusercontent.com/57164712/156378811-8469b090-f682-40ab-91f9-fb3908ca5690.jpg,1
https://user-images.githubusercontent.com/57164712/156378865-0ce062c6-cb84-4941-a2ab-0347d27394c7.jpg,1
https://user-images.githubusercontent.com/57164712/156378902-2954b6bc-ded0-42b1-b4b7-dd197a35cc0a.jpg,2
https://user-images.githubusercontent.com/57164712/156378916-b7a9e9ab-856e-438d-905e-32b94cf9ea20.jpg,2
https://user-images.githubusercontent.com/57164712/156378943-7a1041d4-0d0a-4376-bd00-2890855a2654.jpg,2
https://user-images.githubusercontent.com/57164712/156378961-2e51810d-5fbd-4d11-b105-afb935a0440b.jpg,3
https://user-images.githubusercontent.com/57164712/156378974-17993aec-45f4-453e-aa2c-8631634a5e79.jpg,3
https://user-images.githubusercontent.com/57164712/156378986-6711e1c6-fef4-47fa-870b-baa62b4a6d07.jpg,3
https://user-images.githubusercontent.com/57164712/156378996-7b2658c4-1670-44a0-b1cf-a1300066b8f9.jpg,4
https://user-images.githubusercontent.com/57164712/156379014-1809afe3-7698-4a42-821a-941eb605ff13.jpg,4
https://user-images.githubusercontent.com/57164712/156379025-30784104-34ff-4bea-a49e-91e7931fe54d.jpg,4
https://user-images.githubusercontent.com/57164712/156379038-1afb42aa-0752-4ed1-90ed-e3bd4247a2e3.png,5
https://user-images.githubusercontent.com/57164712/156379047-c2170f7c-295d-409e-8361-f2b891cfa5e7.jpg,5
https://user-images.githubusercontent.com/57164712/156379054-d4f8c0d8-9fe4-4302-81eb-02d77e80f023.jpg,5
https://user-images.githubusercontent.com/57164712/156379062-c0326d1b-4611-4c8d-8905-73b855a661a5.jpg,6
https://user-images.githubusercontent.com/57164712/156379067-736c60db-c3a5-41e9-8675-34fae1910c63.jpg,6
https://user-images.githubusercontent.com/57164712/156379071-a49f0624-78c0-4a7a-9706-0470e2058abb.jpg,6
https://user-images.githubusercontent.com/57164712/156379087-4ec67c57-d266-435c-aa69-82fcfb1560ac.jpg,7
https://user-images.githubusercontent.com/57164712/156379097-5a3f615b-85ad-4b9e-9c78-1ae2540ff7cf.jpg,7
https://user-images.githubusercontent.com/57164712/156379101-5de9ece7-9bbd-4a7a-b793-ea5f1c67609c.jpg,7
https://user-images.githubusercontent.com/57164712/156379117-3ba227eb-2d7d-4be7-b5dd-388c29b3299f.jpg,8
https://user-images.githubusercontent.com/57164712/156379124-07499364-06d8-49e4-8c2e-a3b0bdc459d8.jpg,8
https://user-images.githubusercontent.com/57164712/156379129-0061d4e0-6e53-40c1-869c-c085c1d3e87f.jpg,8
https://user-images.githubusercontent.com/57164712/156379142-49f722cf-893c-40cf-b59b-fd5d7c29dc66.jpg,9
https://user-images.githubusercontent.com/57164712/156379154-156f9f00-9080-4bc9-98b0-74c76f93c95f.jpg,9
https://user-images.githubusercontent.com/57164712/156379163-f371d514-854c-4ca2-869f-5702b9773374.jpg,9
# options.csv
name,price
껍질 제거,1000
시즈닝,2000
양념소스 추가,1000
수비드용 진공포장,1000
뼈 제거,1000
# product_options.csv
product_id,option_id
1,1
1,2
1,3
1,4
1,5
2,1
2,2
2,3
2,4
3,1
3,2
3,3
3,4
4,4
4,5
5,5
6,1
7,1
8,1
# orderstatus.csv
status,
PAID,
PREPARING,
SHIPPEND,
DELIVERD,
ORDER_CANCELLED,
# carts.csv
quantity,user_id,product_id
1,1,1
2,1,2
3,1,3
1,1,4
1,1,5
# cart_options.csv
option_id,cart_id
1,1
2,2
1,3
4,4

# db_uploader.py
import os, django, csv

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

from products.models import *
from orders.models   import *
from carts.models    import *

CSV_PATH_PURCHASE      = 'csv/purchase.csv'
CSV_PATH_MAINCATEGORY  = 'csv/main_categories.csv'
CSV_PATH_SUBCATEGORY   = 'csv/sub_categories.csv'
CSV_PATH_OPTION        = 'csv/options.csv'
CSV_PATH_PRODUCTOPTION = 'csv/product_options.csv'
CSV_PATH_PRODUCT       = 'csv/products.csv'
CSV_PATH_PRODUCTIMAGE  = 'csv/product_images.csv'
CSV_PATH_ORDERSTATUS   = 'csv/orderstatus.csv'
CSV_PATH_CART          = 'csv/carts.csv'
CSV_PATH_CART_OPTION   = 'csv/cart_options.csv'

with open(CSV_PATH_PURCHASE) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None) # 첫번째 행 무시

    for row in data_reader:
        if not Purchase.objects.filter(name=row[0]).exists():
        # 중복된 데이터가 들어가지 않게 함
            name = row[0]
            Purchase.objects.create(name = name)  

with open(CSV_PATH_MAINCATEGORY) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)

    for row in data_reader:
        if not MainCategory.objects.filter(name=row[0]).exists():
            name     = row[0]
            purchase = row[1]
            MainCategory.objects.create(name = name, purchase_id = purchase)

with open(CSV_PATH_SUBCATEGORY) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)

    for row in data_reader:
        if not SubCategory.objects.filter(name=row[0]).exists():
            name          = row[0]
            main_category = row[1]
            SubCategory.objects.create(name = name, main_category_id = main_category)    

with open(CSV_PATH_OPTION) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)

    for row in data_reader:
        if not Option.objects.filter(name=row[0]).exists():
            name  = row[0]
            price = row[1]
            Option.objects.create(name = name, price = price)  

with open(CSV_PATH_PRODUCT) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)

    for row in data_reader:
        if not Product.objects.filter(name=row[0]).exists():
            name         = row[0]
            price        = row[1]
            description  = row[2]
            thumbnail    = row[3]
            sub_category = row[4]
            Product.objects.create(name = name, price = price, description = description, thumbnail = thumbnail, sub_category_id = sub_category)  

with open(CSV_PATH_PRODUCTOPTION) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)

    for row in data_reader:
        if not ProductOption.objects.filter(product_id=row[0], option_id = row[1]).exists():
            product = row[0]
            option  = row[1]
            ProductOption.objects.create(product_id = product, option_id = option)  

with open(CSV_PATH_PRODUCTIMAGE) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)

    for row in data_reader:
        if not ProductImage.objects.filter(image_url=row[0]).exists():
            image_url = row[0]
            product   = row[1]
            ProductImage.objects.create(image_url = image_url, product_id = product)

with open(CSV_PATH_ORDERSTATUS) 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[0]).exists():
            status = row[0]
            OrderStatus.objects.create(status = status)

with open(CSV_PATH_CART) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)

    for row in data_reader:
        if not Cart.objects.filter(quantity=row[0], user_id=row[1], product_id=row[2]).exists():
            quantity = row[0]
            user     = row[1]
            product  = row[2]
            Cart.objects.create(quantity = quantity, user_id = user, product_id = product)

with open(CSV_PATH_CART_OPTION) as in_file:
    data_reader = csv.reader(in_file)
    next(data_reader, None)

    for row in data_reader:
        if not CartOption.objects.filter(option_id=row[0], cart_id=row[1]).exists():
            option = row[0]
            cart   = row[1]
            CartOption.objects.create(option_id = option, cart_id = cart)

  • .gitignoredb_uploader.py,
    /csv/*를 추가해서 깃허브에 올라가지 않도록 한다.
  • python db_uploader.py명령어를 실행하여 DB에 csv로 작성한 데이터를 넣어준다.

0개의 댓글