#!/usr/bin/env python

# -*- coding: utf-8 -*-

import os, sys, django

sys.path.append(".")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo.settings")

django.setup()

from brand.models import *

import json


class ProductDB():

  def __init__(self, model):

    self.model = model

  def create(self, pid, cat1id, cat2id, cat3id, pname, brand, imgurl):

    if self.model == Product:

      p = Product(pid=pid, cat1id=cat1id, cat2id=cat2id, cat3id=cat3id, pname=pname, brand=brand, imgurl=imgurl)

      p.save()


class DataFile():

  def __init__(self, fp):

    self.f = open(fp, 'r')

  def read(self):

    line = self.f.readline().strip()

    return line

  def json_data(self):

    line = self.f.readline().strip()

    data = json.loads(line)

    return data


def main():

  data_dir = "./data/json.txt"

  f = DataFile(data_dir)


  while(True):

    data = f.json_data()

    if not data:  break

    pid = data['id']

    cat1id = data['category1Id']

    cat2id = data['category2Id']

    cat3id = data['category3Id']

    pname = data['productName']

    brand = data['brand']

    imgurl = data['imageUrl']


    p = ProductDB(Product)

    p.create(pid, cat1id, cat2id, cat3id, pname, brand, imgurl)


####################################################

# main

####################################################

if __name__ == '__main__':

  main()



1. 데이터 베이스 프레임 생성 (models.py)

cd demo

vi models.py

# -*- coding: utf-8 -*-

from __future__ import unicode_literals


from django.db import models

from django.db.models import *


# Create your models here.

class Product(Model):

  # primary_key : only one value in this class

  pid = BigIntegerField(primary_key=True)

  # db_index : group value for searching faster

  cat1id = BigIntegerField(db_index=True)

  cat2id = BigIntegerField(db_index=True)

  cat3id = BigIntegerField(db_index=True)

  pname = TextField(default='-')


  status = TextField(default='none')


  def set_status(self, value):

    self.status = value

    self.save()


2. 새로운 모델의 migrations 생성 및 적용

cd ..

python manage.py makemigrations demo

python manage.py migrate demo


3. 데이터 베이스 생성

vi test.db

12345 001 002 003 Poorman T-shirts

12346 001 002 004 Poorman Boots

12347 001 003 003 Poorman Cap

12348 002 003 004 Poorman Watch

vi make_db.py

# -*- coding: utf-8 -*-

from __future__ import unicode_literals


import os, sys, django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

django.setup()

from catetc.models import *


class ModelObjects():

  def __init__(self, model):

    self.model = model


  def create(self, line):

    line = line.strip()

    words = line.split('\t')

    pid = words[0]

    cat1id = words[1]

    cat2id = words[2]

    cat3id = words[3]

    pname = words[4]

    self.model.objects.create(pid=pid, cat1id=cat1id, cat2id=cat2id, cat3id=cat3id, pname=pname)


  def delete(self):

    self.model.objects.all().delete()



def main():

  db = ModelObjects(Product)

  db.delete()

  for line in sys.stdin:

    line = line.strip()

    if not line:  continue


    db.create(line)


# main

if __name__ == '__main__':

  main()

cat test.db | python make_db.py


5. admin (관리자) 모드에서 볼 수 있게 admin 등록

vi demo/admin.py

# -*- coding: utf-8 -*-

from __future__ import unicode_literals


from django.contrib import admin

from .models import *


# Register your models here.

admin.site.register(Product)


6. admin 홈페이지에서 데이터 베이스 확인

http://~:8080/admin


+ Recent posts