简体   繁体   English

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

[英]Django : How to import a data from csv to database without using form?

I'm very new to Django, actually very new to coding too. 我对Django很陌生,实际上对编码也很陌生。 I know it's a silly question, but I have no idea how to make this. 我知道这是一个愚蠢的问题,但是我不知道如何做到这一点。

I wanted to import some of data from local csv file and store to database (mine is mysql) without create an upload form (almost of tutorials I've found from google). 我想从本地的csv文件导入一些数据并存储到数据库(我的是mysql),而无需创建上传表单(我从Google找到的教程几乎都是如此)。

I'm very confused of MVC model eg where's the part of handling csv should stand? 我对MVC模型非常困惑,例如,处理csv的部分应该放在哪里? view or model? 视图或模型? and also I have to create a function to cut undesired fields from csv. 并且我还必须创建一个函数来从csv中剪切不想要的字段。 where should I put that code in ? 我应该把代码放在哪里?

Here's my model 这是我的模特

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)

Here's a code to remove unwanted fields (standalone file) 这是删除不需要的字段(独立文件)的代码

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()

the second file will give a router_ip, neighbor_ip and asn. 第二个文件将给出router_ip,neighbor_ip和asn。 Do I have to create a new class in model to keep the data? 我必须在模型中创建一个新类来保留数据吗? can I add the data to a particular class instead of create a new one eg store router_ip to Class Peer, neighbor_ip to Class PeerNeighbor and store asn to Class ASN. 我可以将数据添加到一个特定的类中,而不是创建一个新的类,例如,将router_ip存储到Class Peer,将neighbor_ip存储到Class PeerNeighbor,并将asn存储到Class ASN。

these are a new class for purpose of keeping data from csv (inside model) but it didn't work. 这些是用于保留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()

for update from executing a script, it gave me an errors 从执行脚本进行更新,它给了我一个错误

(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>

You can create your own script and run with python manage.py shell command: 您可以创建自己的脚本并使用python manage.py shell命令运行:
Your script should be something like this: 您的脚本应该是这样的:

#!/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])

Take a look to my Example in Github 看看我在Github中的示例
On the other hand, I recommend you take a look to this Answer , here you will find more information about how works with .csv files in Django. 另一方面,我建议您看一下“ 答案” ,在这里您将找到有关如何在Django中使用.csv文件的更多信息。

  • Create a REST API to accept list of records to be inserted 创建一个REST API以接受要插入的记录列表
  • Write your cleanup scripts in the View that handles the request 在用于处理请求的视图中编写清理脚本
  • Use a sample script to read from CSV and form JSON compatible with the API, and send request to your API endpoint with formed JSON as data 使用示例脚本从CSV读取并形成与API兼容的JSON,并以形成的JSON作为数据将请求发送到您的API端点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM