Python 使用django将csv文件中的数据添加到数据库
我有Django模型,如下所示:Python 使用django将csv文件中的数据添加到数据库,python,django,csv,Python,Django,Csv,我有Django模型,如下所示: class TraxioRelations(models.Model): corgemeente = models.CharField(max_length=100, null=True) coradres = models.CharField(max_length=255, null=True) corhuisnr = models.CharField(max_length=10, null=True, blank=True) COP
class TraxioRelations(models.Model):
corgemeente = models.CharField(max_length=100, null=True)
coradres = models.CharField(max_length=255, null=True)
corhuisnr = models.CharField(max_length=10, null=True, blank=True)
COPY master_traxiorelations(corgemeente, coradres, corhuisnr)
FROM '/path/to/file.csv' CSV HEADER delimiter ';' encoding 'ISO-8859-1';
[2017-08-21 15:50:49] [22P04] ERROR: extra data after last expected column
id = models.AutoField(primary_key=True)
该模型在数据库中创建一个id为id
的表,如下所示:
现在我想将csv文件中的记录添加到该模型中。csv文件如下所示:
class TraxioRelations(models.Model):
corgemeente = models.CharField(max_length=100, null=True)
coradres = models.CharField(max_length=255, null=True)
corhuisnr = models.CharField(max_length=10, null=True, blank=True)
COPY master_traxiorelations(corgemeente, coradres, corhuisnr)
FROM '/path/to/file.csv' CSV HEADER delimiter ';' encoding 'ISO-8859-1';
[2017-08-21 15:50:49] [22P04] ERROR: extra data after last expected column
id = models.AutoField(primary_key=True)
我使用copy
命令如下:
class TraxioRelations(models.Model):
corgemeente = models.CharField(max_length=100, null=True)
coradres = models.CharField(max_length=255, null=True)
corhuisnr = models.CharField(max_length=10, null=True, blank=True)
COPY master_traxiorelations(corgemeente, coradres, corhuisnr)
FROM '/path/to/file.csv' CSV HEADER delimiter ';' encoding 'ISO-8859-1';
[2017-08-21 15:50:49] [22P04] ERROR: extra data after last expected column
id = models.AutoField(primary_key=True)
当我执行它时,我得到一个错误,如下所示:
class TraxioRelations(models.Model):
corgemeente = models.CharField(max_length=100, null=True)
coradres = models.CharField(max_length=255, null=True)
corhuisnr = models.CharField(max_length=10, null=True, blank=True)
COPY master_traxiorelations(corgemeente, coradres, corhuisnr)
FROM '/path/to/file.csv' CSV HEADER delimiter ';' encoding 'ISO-8859-1';
[2017-08-21 15:50:49] [22P04] ERROR: extra data after last expected column
id = models.AutoField(primary_key=True)
但是,当我向copy
命令添加id
时,因此copy master\u传输关系(id、corgemeente、coradres、corhuissr)
和csv
文件:
那么它工作得很好
如何将文件中的数据添加到数据库中,而不将id添加到复制命令和csv文件中?我通过将id添加到模型中解决了此问题,如下所示:
class TraxioRelations(models.Model):
corgemeente = models.CharField(max_length=100, null=True)
coradres = models.CharField(max_length=255, null=True)
corhuisnr = models.CharField(max_length=10, null=True, blank=True)
COPY master_traxiorelations(corgemeente, coradres, corhuisnr)
FROM '/path/to/file.csv' CSV HEADER delimiter ';' encoding 'ISO-8859-1';
[2017-08-21 15:50:49] [22P04] ERROR: extra data after last expected column
id = models.AutoField(primary_key=True)
添加id=models.AutoField(primary_key=True)
可以自动增加id。因此,我的模型如下所示:
class TraxioRelations(models.Model):
id = models.AutoField(primary_key=True)
corgemeente = models.CharField(max_length=100, null=True)
coradres = models.CharField(max_length=255, null=True)
corhuisnr = models.CharField(max_length=10, null=True, blank=True)
然后,copy
命令不在csv文件中添加id列
COPY master_traxiorelations(corgemeente, coradres, corhuisnr)
FROM '/path/to/file.csv' CSV HEADER delimiter ';' encoding 'ISO-8859-1';
如果其他人有同样的问题,希望这能有所帮助。我通过向模型中添加
id
解决了它,如下所示:
class TraxioRelations(models.Model):
corgemeente = models.CharField(max_length=100, null=True)
coradres = models.CharField(max_length=255, null=True)
corhuisnr = models.CharField(max_length=10, null=True, blank=True)
COPY master_traxiorelations(corgemeente, coradres, corhuisnr)
FROM '/path/to/file.csv' CSV HEADER delimiter ';' encoding 'ISO-8859-1';
[2017-08-21 15:50:49] [22P04] ERROR: extra data after last expected column
id = models.AutoField(primary_key=True)
添加id=models.AutoField(primary_key=True)
可以自动增加id。因此,我的模型如下所示:
class TraxioRelations(models.Model):
id = models.AutoField(primary_key=True)
corgemeente = models.CharField(max_length=100, null=True)
coradres = models.CharField(max_length=255, null=True)
corhuisnr = models.CharField(max_length=10, null=True, blank=True)
然后,copy
命令不在csv文件中添加id列
COPY master_traxiorelations(corgemeente, coradres, corhuisnr)
FROM '/path/to/file.csv' CSV HEADER delimiter ';' encoding 'ISO-8859-1';
如果其他人也有同样的问题,希望这能有所帮助。我建议使用一个名为django导入导出的外部库。文档链接如下:我建议使用一个名为django导入导出的外部库。文档链接如下: