(生鲜项目)04. 导入数据
第一步: copy文件, 设置路径
把brands和goods文件夹copy到以下路径, 并配置好相应字段的upload to
第二步: 导入GoodsCategory
准备独立使用models模块, 来一次性导入大量的 "goods.models.GoodsCategory" 数据
为什么不先导入Goods? 因为Goods里有GoodsCategory的外键
先把源文件 category_data.py 和 product_data.py 复制到如下路径, 然后手动新建import_category_data和import_product_data
import_category_data.py
# 独立使用django的model模块
import sys,os
# from users.models import UserProfile
# 如果在配置DJANGO_SETTINGS_MODULE之前导入了与django有关的模块,会报错,见下面的截图:
# "You must either define the environment variable DJANGO_SETTINGS_MODULE"
# 把项目根路径加到系统环境变量中去
pwd=os.path.dirname(os.path.abspath(__file__))
sys.path.append(pwd+"../")
# 导入settings文件
os.environ.setdefault("DJANGO_SETTINGS_MODULE","MxShop.settings")
# django初始化
import django
django.setup()
from goods.models import GoodsCategory
from db_tools.data.category_data import row_data
for lev1_cat in row_data:
lev1_instance = GoodsCategory()
lev1_instance.name = lev1_cat["name"]
lev1_instance.code = lev1_cat["code"]
lev1_instance.category_type = 1
lev1_instance.save()
for lev2_cat in lev1_cat["sub_categorys"]:
lev2_instance = GoodsCategory()
lev2_instance.name = lev2_cat["name"]
lev2_instance.code = lev2_cat["code"]
lev2_instance.category_type = 2
lev2_instance.parent_category = lev1_instance
lev2_instance.save()
for lev3_cat in lev2_cat["sub_categorys"]:
lev3_instance = GoodsCategory()
lev3_instance.name = lev3_cat["name"]
lev3_instance.code = lev3_cat["code"]
lev3_instance.category_type = 3
lev3_instance.parent_category = lev2_instance
lev3_instance.save()
接下来执行该文件, 就可以发现数据库中自动填充了很多数据, 一共120条, 一级类目有8个
第三步: 导入Goods
准备独立使用models模块, 来一次性导入
import_product_data.py
# 独立使用django的model模块
import sys,os
# 把项目根路径加到系统环境变量中去
pwd=os.path.dirname(os.path.abspath(__file__))
sys.path.append(pwd+"../")
# 导入settings文件
os.environ.setdefault("DJANGO_SETTINGS_MODULE","MxShop.settings")
# django初始化
import django
django.setup()
from goods.models import Goods,GoodsCategory,GoodsImage
from db_tools.data.product_data import row_data
for goods_detail in row_data:
goods = Goods()
goods.name = goods_detail["name"]
goods.market_price = float(int(goods_detail["market_price"].replace("¥","").replace("元","")))
goods.shop_price = float(int(goods_detail["sale_price"].replace("¥","").replace("元","")))
goods.goods_brief = goods_detail["desc"] if goods_detail["desc"] is not None else ""
goods.goods_desc = goods_detail["goods_desc"] if goods_detail["goods_desc"] is not None else ""
goods.goods_front_image = goods_detail["images"][0] if goods_detail["images"] else ""
category_name = goods_detail["categorys"][-1]
category = GoodsCategory.objects.filter(name=category_name)
if category:
goods.category = category[0]
goods.save()
for goods_image in goods_detail["images"]:
goods_image_instance = GoodsImage()
goods_image_instance.image = goods_image
goods_image_instance.goods = goods
goods_image_instance.save()
接下来去数据库和xadmin后台验证, 检查是否是52条数据
可以发现, 商品描述里面的图片是破图, 是因为还没有配置MEDIA, 现在去设置
settings.py
MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR,'media')
urls.py
from django.conf.urls import url import xadmin from MxShop.settings import MEDIA_ROOT from django.views.static import serve urlpatterns = [ url(r'^xadmin/', xadmin.site.urls), url(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT}), ]
再刷新页面, 破图恢复正常了
----- over ------