使用导入脚本填充数据库表-Django

时间:2019-04-30 02:01:10

标签: python django

我是Django的新手,我看不到任何“直截了当”的方式来完成我想要的事情。

我有一个新的django应用,并且已经创建了数据库的初始迁移。它有一张桌子。一切似乎都很好,我创建了一个python脚本来导入数据。运行它后,我立即收到以下错误:

  

django.core.exceptions.ImproperlyConfigured:请求的设置   INSTALLED_APPS,但未配置设置。你必须   定义环境变量DJANGO_SETTINGS_MODULE或调用   settings.configure(),然后再访问设置。

模型

from django.db import models


class Person(models.Model):
    playerID = models.TextField()
    birthYear = models.IntegerField()
    birthMonth = models.IntegerField()
    birthDay = models.IntegerField()
    birthCountry = models.TextField()
    birthState = models.TextField()
    birthCity = models.TextField()
    deathYear = models.IntegerField()
    deathMonth = models.IntegerField()
    deathDay = models.IntegerField()
    deathCountry = models.TextField()
    deathState = models.TextField()
    deathCity = models.TextField()
    nameFirst = models.TextField()
    nameLast = models.TextField()
    nameGiven = models.TextField()
    weight = models.IntegerField()
    height = models.IntegerField()
    bats = models.TextField()
    throws = models.TextField()
    debut = models.DateField()
    finalGame = models.DateField()
    retroID = models.TextField()
    bbrefID = models.TextField()

导入脚本

import pandas as pd
from api.models import Person

people_csv = pd.read_csv('data/People.csv')

people_objects = []
for p in people_csv.iterrows():
    person = Person()
    person.playerID = p.playerID
    person.birthYear = p.birthYear
    person.birthMonth = p.birthMonth
    person.birthDay = p.birthDay
    person.birthCountry = p.birthCountry
    person.birthState = p.birthState
    person.birthCity = p.birthCity
    person.deathYear = p.deathYear
    person.deathMonth = p.deathMonth
    person.deathDay = p.deathDay
    person.deathCountry = p.deathCountry
    person.deathState = p.deathState
    person.deathCity = p.deathCity
    person.nameFirst = p.nameFirst
    person.nameLast = p.nameLast
    person.nameGiven = p.nameGiven
    person.weight = p.weight
    person.height = p.height
    person.bats = p.bats
    person.throws = p.throws
    person.debut = p.debut
    person.finalGame = p.finalGame
    person.retroID = p.retroID
    person.bbrefID = p.bbrefID
    people_objects.append(p)

Person.objects.bulk_create(people_objects)

我的假设是我需要将我的导入脚本添加到setting.py或其他模块中的某个位置。我对要添加的位置 不清楚。

文件夹结构

enter image description here

0 个答案:

没有答案