Django:如何在不使用表单的情况下将数据从csv导入数据库?

时间:2018-11-09 10:15:06

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

我是Django的新手,实际上也是编码的新手。 我知道这是一个愚蠢的问题,但是我不知道该怎么做。

我想从本地csv文件中导入一些数据并将其存储到数据库(我的是mysql),而无需创建上传表单(我从Google找到的教程几乎都是如此)。

我对MVC模型非常困惑,例如处理csv的部分应该放在哪里?视图或模型?并且我还必须创建一个函数来从csv中剪切不想要的字段。我应该将该代码放在哪里?

这是我的模特

from __future__ import unicode_literals
import csv, io
from django.conf import settings
from django.db import models

#from django_countries.fields import CountryField

class ASN(models.Model):
    num = models.IntegerField(primary_key=True)
    owner = models.CharField(max_length=50, null=True)
    # Using countryfield to convert from country code to name
    countryCode = models.CharField(max_length=5)
    name = models.CharField(max_length=100, null=True)
    #countryName = CountryField()

    def __str__(self):
        return str(self.owner) + " " + str(self.num) + " " + str(self.countryCode)

class Host(models.Model):
    name = models.CharField(max_length=20)
    id = models.IntegerField(primary_key=True)

    def __str__(self):
        return str(self.id) + " " + str(self.name)

class Peer(models.Model):
    router_ip = models.CharField(max_length=20, primary_key=True)
    bgp_state = models.IntegerField(default=0) 
    as_num = models.ForeignKey('ASN', on_delete=models.CASCADE)
    host_id = models.ForeignKey('Host', on_delete=models.CASCADE)

    def __str__(self):
        return str(self.host_id) + ' ' + str(self.router_ip) + ' ' + str(self.as_num) + ' ' + str(self.bgp_state)

class PeerNeighbor(models.Model):
    neighbor_ip = models.CharField(max_length=20, primary_key=True)
    router_ip = models.ForeignKey('Peer', on_delete=models.CASCADE)

    def __str__(self):
        return str(self.router_ip) + ' ' + str(self.neighbor_ip)

这是删除不需要的字段(独立文件)的代码

import csv

txt_file_id = r'MR-SG1-BGPPEER.txt'
txt_file_AS = r'show_AS.txt'
csv_file_out = r'file_out.csv'
peer = []
bgp_peer = []
remote_router_id = []
AS_number = []
AS = []
router_ip = []

def main():
    readInput(txt_file_id, txt_file_AS)
    writeOutput(csv_file_out)

def readInput(filename_1, filename_2):
    with open(filename_1, newline='') as csvfile_1:
        spamreader1 = csv.reader(csvfile_1, delimiter=' ', quotechar=" ")
        for row in spamreader1:
            row = ','.join(row)
            row = row.split(',')
            bgp_peer = row[0]
            remote_router_id = row[3]
            bgp_peer = split_list(bgp_peer)
            peer.append(bgp_peer) #store results into list
            router_ip.append(remote_router_id)
        #print(peer)

    with open(filename_2, newline='') as csvfile_2:
        spamreader2 = csv.reader(csvfile_2, delimiter=' ', quotechar=" ")
        for row in spamreader2:
            row = ','.join(row)
            row = row.split(',')
            AS_number = row[3]
            AS.append(AS_number) #store results into list
        #print(AS)
    print(peer, AS)

def writeOutput(filename):
    with open(filename, 'w') as outputFile:
        wr = csv.writer(outputFile, quoting=csv.QUOTE_ALL)
        wr.writerow(zip(router_ip, peer, AS))

def split_list(inputlist):
    string = inputlist.split(".")
    count = 0
    for i in string:
        count+=1
    bgp_peer_ip = string[5:count]
    bgp_peer_ip = '.'.join(bgp_peer_ip)
    return(bgp_peer_ip)   
main()

第二个文件将提供router_ip,neighbor_ip和asn。我必须在模型中创建一个新类来保留数据吗?我可以将数据添加到特定的类而不是创建一个新的类例如将router_ip存储到Class Peer,将neighbor_ip存储到Class PeerNeighbor,并将asn存储到Class ASN。

这是一个新类,用于保留csv(内部模型)中的数据,但不起作用。

class dataFromFile(models.Model):
    router_ip = models.CharField(max_length=20, primary_key = True)
    as_num = models.IntegerField(default=0)
    neighbor_ip = models.CharField(max_length=20)
    objects = models.Manager()

def import_db(request):
    f = open('/home/Jobs/Peering_db/file_out.csv')
    for line in f:
        line = line.split(',')
        tmp = dataFromFile.objects.create()
        tmp.router_ip = line[0]
        tmp.neighbor_ip = line[1]
        tmp.as_num = line[2]
        tmp.save()
    f.close()

用于更新 执行脚本后,它给了我一个错误

(env) bowbth@bowbth:~/django-apps/mysite$ python manage.py shell
Python 3.6.6 (default, Sep 12 2018, 18:26:19) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> exec(open('import_data_csv.py').read())
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<string>", line 16, in <module>

2 个答案:

答案 0 :(得分:1)

您可以创建自己的脚本并使用python manage.py shell命令运行:
您的脚本应该是这样的:

#!/usr/bin/env python

"""
    Script to import data from .csv file to Model Database DJango
    To execute this script run: 
                                1) manage.py shell
                                2) exec(open('file_name.py').read())
"""

import csv
from AppName.models import Model1, Model2 

CSV_PATH = '../../your_file_name.csv'      # Csv file path  


with open(CSV_PATH, newline='') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=';', quotechar=';')
    for row in spamreader:
        Model.objects.create(... Attributes here ...)
        # Example -> Book.objects.create(ISBNCode=row[0], title=row[1], author=row[2])
  

看看我的Example in Github   
另一方面,我建议您看一下   Answer,   在这里,您将找到有关如何使用.csv文件的更多信息。   Django。

答案 1 :(得分:0)

  • 创建REST API以接受要插入的记录列表
  • 在处理请求的视图中写入清理脚本
  • 使用示例脚本从CSV读取并形成与API兼容的JSON,然后以形成的JSON作为数据将请求发送到您的API端点