如何从Excel文件填充Django Manytomany数据库

时间:2019-02-15 01:29:59

标签: django python-3.x django-models sqlite

我正在尝试将数据从excel文件传输到sqlite3数据库中的manytomany表中。

model.py

from django.db import models


class Major(models.Model):
    name = models.CharField(max_length=30, db_index=True)

class School(models.Model):
    name = models.CharField(max_length=50, db_index=True)
    majors = models.ManyToManyField(Major)


class professor(models.Model):
    ProfessorIDS = models.IntegerField()
    ProfessorName = models.CharField(max_length=100)
    ProfessorRating = models.DecimalField(decimal_places=2,max_digits=4)
    NumberofRatings = models.CharField(max_length=50)
    #delete major from the model  
    school = models.ForeignKey(School , on_delete=models.CASCADE)
    major = models.ForeignKey(Major , on_delete=models.CASCADE)

    def __str__(self):
        return self.ProfessorName

填充脚本

# populate.py, school_major_link
import os
import django
from django_xlspopulator.populator import Populator
os.environ.setdefault('DJANGO_SETTINGS_MODULE','blog_project.settings')
django.setup()
from locate.models import Major

pop = Populator('C:/Users/David/Desktop/db_setup/School_Majors.xlsx', school_majors)
pop.populate()

尝试运行脚本时出现错误消息

Traceback (most recent call last):
  File "populate_school_major.py", line 9, in <module>
    pop = Populator('C:/Users/David/Desktop/db_setup/School_Majors.xlsx', school_majors)
NameError: name 'school_majors' is not defined

但这很有意义,因为此脚本在“模型”部分中查找类名,而不是表的名称,所以我不太确定如何填充正确的表,请注意,我已经有了一个已使用此脚本填充了名为majors的表,但由于django通过变量和许多单独的类建立了许多联系,因此我陷入了困境。

我尝试使用上面的填充脚本,但是注意到这无法正常工作,因为它正在查找类,而不是将数据库表另存为。 (在sqlite3中,manytomanyfield专业的表名称称为school_majors。)

如果有人对如何填充数据库有任何建议,那就太好了。

下面的数据库表名称图片。 enter image description here 下面的Excel文件

enter image description here

2 个答案:

答案 0 :(得分:1)

当您在Django中定义ManyToManyField时,它会在后台创建一个模型来存储映射。

您可以使用ManyToManyField的(x1 & x2)属性访问此模型。在您的情况下,这将是:

through

您当前的脚本失败了,因为locate.models.School.majors.through 根本不是一个已定义的名称-尝试用对您要填充的模型的引用替换它:

school_majors

如果这不起作用,您可能希望考虑在ManyToManyField(如explained in the Django docs)上定义一个显式pop = Populator('C:/Users/David/Desktop/db_setup/School_Majors.xlsx', locate.models.School.majors.through) pop.populate() 模型,然后在through实例化中指定它。

祝你好运!

答案 1 :(得分:0)

正如不冷不热地说的那样,设置通过允许我创建一个类,我可以用我的脚本引用该类,严格来说,可以通过在模型中定位一个类来填充该类。py

from django.db import models

class Major(models.Model):
    name = models.CharField(max_length=30, db_index=True)

class School(models.Model):
    name = models.CharField(max_length=50, db_index=True)
    school_Major_merge = models.ManyToManyField(Major, through='School_Major')

class School_Major(models.Model):
    major = models.ForeignKey(Major, on_delete=models.CASCADE)
    school = models.ForeignKey(School, on_delete=models.CASCADE)


class professor(models.Model):
    ProfessorIDS = models.IntegerField()
    ProfessorName = models.CharField(max_length=100)
    ProfessorRating = models.DecimalField(decimal_places=2,max_digits=4)
    NumberofRatings = models.CharField(max_length=50)
    #delete major from the model  
    school = models.ForeignKey(School , on_delete=models.CASCADE)
    major = models.ForeignKey(Major , on_delete=models.CASCADE)

    def __str__(self):
        return self.ProfessorName