关于python 3.x:如何从excel文件填充django manytomany数据库

How to populate a django manytomany database from excel file

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

型号.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

填充脚本

1
2
3
4
5
6
7
8
9
10
# 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()

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

1
2
3
4
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

但这是有意义的,因为这个脚本在models节中查找类名与表名的关系,所以我不太确定如何填充正确的表,请注意,我已经有了一个名为majors的表,该表已经使用这个脚本填充了,但是由于django通过变量vers和分开上课,我困了。

我尝试使用上面的填充脚本,但注意到这不起作用,因为它正在定位类,与数据库表的保存方式相反。(在sqlite3中,许多专业的表名称为school_majors)。

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

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

氧化镁


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

您可以使用manytomanyfield的through属性访问此模型。在您的情况下,这将是:

1
locate.models.School.majors.through

当前脚本失败,因为school_majors根本不是已定义的名称-请尝试将其替换为对要填充的模型的引用:

1
2
pop = Populator('C:/Users/David/Desktop/db_setup/School_Majors.xlsx', locate.models.School.majors.through)
pop.populate()

如果这不起作用,您可能希望考虑在manytomanyfield(如Django文档中所述)上定义一个显式的through模型,然后在Populator实例中指定该模型。

祝你好运!


正如Lukewarm所说,设置through允许我创建一个类,我可以用我的脚本引用这个类,严格来说,通过在model.py中定位一个类来填充它。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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