代码之家  ›  专栏  ›  技术社区  ›  mwilson

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

  •  0
  • mwilson  · 技术社区  · 7 年前

    我是Django的新手,我没有看到任何“直接”的方式来做我想做的事情。

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

    德扬戈。果心例外。配置不当:请求的设置 已安装应用程序,但未配置设置。你必须 定义环境变量DJANGO_SETTINGS_MODULE或call 设置。在访问设置之前配置()。

    模型

    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)
    

    我的假设是,我需要在设置中的某个位置添加导入脚本。py或其他模块。我只是不完全清楚 哪里 和 什么 我需要补充一点。

    文件夹结构

    enter image description here

    0 回复  |  直到 7 年前