django csv import automatically












0















i'm Marcello from italy, i'm new in python and django. I ask a question to get info for my project in django.



I've created a project that have two tables, my problem is that how get data automatically from csv, i will create an ftp server that stored csv.txt and update the tables (for example every day).



Now i update with Django import / export manually. I would like that django read csv files and update db then delete.



Thank you very much for all your support!!!



my models.py:



from django.db import models
from django.urls import reverse



class AnagraficaCliente(models.Model):
codice_cliente = models.CharField(max_length=20, primary_key=True,
null=False, unique=True)
ragione_sociale = models.CharField(max_length=40)
ragione_sociale_dest = models.CharField(max_length=40)
nome = models.CharField(max_length=20, blank=True)
cognome = models.CharField(max_length=20, blank=True)
ragione_sociale = models.CharField(max_length=20)
indirizzo = models.TextField(blank=True)
cap = models.CharField(max_length=5, blank=True)
piva = models.CharField(max_length=11, blank=True)
vatnumber = models.CharField(max_length=13, blank=True)
ragione_sociale_dest = models.CharField(max_length=40)
indirizzo_dest = models.TextField(null=True)

def __str__(self):
#return self.ragione_sociale + " " + self.codice_cliente
return self.ragione_sociale_dest + " " + self.indirizzo_dest

class Meta:
verbose_name = "AnagraficaCliente"
verbose_name_plural = "AnagraficaClienti"




class Tracking(models.Model):
track = models.CharField(max_length=10, null=False, unique=True,
primary_key=True)
passaggio1 = models.CharField(max_length=50, blank=True)
passaggio2 = models.CharField(max_length=50, blank=True)
passaggio3 = models.CharField(max_length=50, blank=True)
passaggio4 = models.CharField(max_length=50, blank=True)
passaggio5 = models.CharField(max_length=50, blank=True)
consegna = (
('C', 'Consegnato'),
('N', 'Non consegnato'),
)
consegnato = models.CharField(max_length=1, choices=consegna)

#consegnato = models.BooleanField(blank=True)
#esito = models.CharField(max_length=10, blank=True)
flag = models.CharField(max_length=2, blank=True)
AnagraficaCliente = models.ForeignKey(AnagraficaCliente,
on_delete=models.CASCADE, related_name='trackings')

def __str__(self):
return self.track

class Meta:
verbose_name = "Tracking"
verbose_name_plural = "Trackings"


I make a script that read from trackingtest.csv and store in result_str_csv_anag_index_8_list then read from anagraficatest.csv and insert in mysql, after this,i get a query from table tracking_anagraficacliente (
previously entered in mysql previously) and i get last field data to store in result_str_query_anag_index_8_list then compare result_str_csv_anag_index_8_list in result_str_query_anag_index_8_list, if foreign key is correct.



All of this work with a cron in linux but i'd like to implement inside django (in backend) with alert, (for example button red or green if the insert work fine).



I hope to explain well what I have done, i'm beginner ;)



Thank you.



#!/usr/bin/python3

import csv
import MySQLdb, time

sql_anag= """
INSERT INTO tracking_anagraficacliente
(codice_cliente,nome,cognome,ragione_sociale,indirizzo,cap,piva,vatnumber,ragione_sociale_dest,indirizzo_dest)
VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
-- no primary key aggiornata

nome = VALUES(nome),
cognome = VALUES(cognome),
ragione_sociale = VALUES(ragione_sociale),
indirizzo = VALUES(indirizzo),
cap = VALUES(cap),
piva = VALUES(piva),
vatnumber = VALUES(vatnumber),
ragione_sociale_dest = VALUES(ragione_sociale_dest),
indirizzo_dest = VALUES(indirizzo_dest);
"""
sql_track= """
INSERT INTO tracking_tracking

VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
-- no primary key aggiornata

passaggio1 = VALUES(passaggio1),
passaggio2 = VALUES(passaggio2),
passaggio3 = VALUES(passaggio3),
passaggio4 = VALUES(passaggio4),
passaggio5 = VALUES(passaggio5),
consegnato = VALUES(consegnato),
flag = VALUES(flag),
AnagraficaCliente_id = VALUES(AnagraficaCliente_id);
"""
query_id_anag = """ select codice_cliente from tracking_anagraficacliente; """
#query_id_track = """ select AnagraficaCliente_id from tracking_tracking; """


db = MySQLdb.connect(
host="localhost", # your host
user="xxxxxxx", # your username
passwd="xxxxxx", # your password
db="lm_cms") # name of the data base

cur = db.cursor()

with open('trackingtest.csv') as csvfile: #
readCSVQuery = csv.reader(csvfile, delimiter=',')
next (readCSVQuery)
list_csv_tracking =
for i in readCSVQuery:
trackAnagraficaId = i[8]
list_csv_tracking.append(trackAnagraficaId)
result_str_csv_track_index_8 = str(list_csv_tracking).replace(",",
"").replace("[", "").replace("]", "")
result_str_csv_anag_index_8_list = result_str_csv_track_index_8.split(' ')
print ("Risultato CSV Tracking Foreign
Key",result_str_csv_anag_index_8_list)

def insert_anagrafica():
with open('anagraficatest.csv') as csvfile: #read CSV anagraficatest
readCSV = csv.reader(csvfile, delimiter=',')
next (readCSV) # Jump first line
for row_anag in readCSV:
try:
cur.execute (sql_anag, (row_anag)) #Insert data on mysql
db.commit() # Commit your changes in the database
print(row_anag)
except (MySQLdb.Error, MySQLdb.Warning) as e: #Exception
print(e)
db.rollback() # Rollback in case there is any error
db.close()

def query_anag():
cur.execute(query_id_anag) #Execute query to get codice_cliente from tracking_anagraficacliente
results_query_id_anag = cur.fetchall() # Get all data from query and assign to results_query_id_anag
result_query_anag = str(results_query_id_anag).replace("(", "").replace(")", "").replace(",", "")# Delete ( ) , from results_query_id_anag and convert to string
result_str_query_anag_index_8_list = result_query_anag.split(' ') # Convert in string from result_query_anag to result_str_query_anag_index_8_list
print ("Risultato Query anagrafica Codice cliente ",result_str_query_anag_index_8_list)
return result_str_query_anag_index_8_list #return value

def insert_tracking():
with open('trackingtest.csv') as csvfile: #Read Csv
readCSV = csv.reader(csvfile, delimiter=',') #Read csv file
next (readCSV) #Jump first line
#print (results_query_id_anag)
#compare if AnagraficaClienteID in trackingtest.csv is present in mysql tracking_aanagraficacliente Codice Cliente
#,if present make the insert sql_track else no
for i in result_str_csv_anag_index_8_list: #compare element lists in result_str_csv_anag_index_8_list is present in result_str_query_anag_index_8_list
if i in result_str_query_anag_index_8_list:
for row_track in readCSV: # Read all the line in trackingtest.csv
try:
#print(row_track[0])
#print(row_track)

cur.execute (sql_track, (row_track)) #Insert data trackingtest.csv in tracking_tracking
db.commit() # Commit your changes in the database
print ("Insert effettuata con successo ", row_track)
except (MySQLdb.Error, MySQLdb.Warning) as e:
print (e, "Riga non inserita: ",row_track)
#db.close() # disconnect from server
else:
print ("Attenzione, una o più righe non sono state inserite")


insert_anagrafica()
time.sleep(2)
(result_str_query_anag_index_8_list) = query_anag() #value passed from def query_anag() to insert_tracking
time.sleep(2)
insert_tracking()









share|improve this question

























  • when you say gettting csv automatically, how? ftp? local file? http/s? also, can you give example of code you tried? a sample of the csv to be imported? is this an insert and replace or an upsert (update/insert)?

    – Incognos
    Nov 24 '18 at 8:54











  • I thought update db through an ftp server, all data that uploaded on ftp with upsert command. My question is, whatis the best practise? Through script in python that update and insert on mysql, (with an cron job) or there is anothe solutions with django? Thank you.

    – Marcello Colangelo
    Nov 25 '18 at 12:34













  • You are collecting the file from a ftp server (hopefully sftp) then opening said file and running through the db to see if the record exists, then update and if not insert? cron is the simplest and least bulletproof way to implement. would recommend using cronitor if you go down that route

    – Incognos
    Nov 25 '18 at 12:37











  • could you please post your code? we cannot write all the code for you from scratch? you have to show us what you attempted so far.

    – Incognos
    Nov 25 '18 at 12:37











  • no, no i'm sorry i don't want that you write form me my code, i ask the best way to do, in this week i post the code. Thank you.

    – Marcello Colangelo
    Nov 25 '18 at 14:22
















0















i'm Marcello from italy, i'm new in python and django. I ask a question to get info for my project in django.



I've created a project that have two tables, my problem is that how get data automatically from csv, i will create an ftp server that stored csv.txt and update the tables (for example every day).



Now i update with Django import / export manually. I would like that django read csv files and update db then delete.



Thank you very much for all your support!!!



my models.py:



from django.db import models
from django.urls import reverse



class AnagraficaCliente(models.Model):
codice_cliente = models.CharField(max_length=20, primary_key=True,
null=False, unique=True)
ragione_sociale = models.CharField(max_length=40)
ragione_sociale_dest = models.CharField(max_length=40)
nome = models.CharField(max_length=20, blank=True)
cognome = models.CharField(max_length=20, blank=True)
ragione_sociale = models.CharField(max_length=20)
indirizzo = models.TextField(blank=True)
cap = models.CharField(max_length=5, blank=True)
piva = models.CharField(max_length=11, blank=True)
vatnumber = models.CharField(max_length=13, blank=True)
ragione_sociale_dest = models.CharField(max_length=40)
indirizzo_dest = models.TextField(null=True)

def __str__(self):
#return self.ragione_sociale + " " + self.codice_cliente
return self.ragione_sociale_dest + " " + self.indirizzo_dest

class Meta:
verbose_name = "AnagraficaCliente"
verbose_name_plural = "AnagraficaClienti"




class Tracking(models.Model):
track = models.CharField(max_length=10, null=False, unique=True,
primary_key=True)
passaggio1 = models.CharField(max_length=50, blank=True)
passaggio2 = models.CharField(max_length=50, blank=True)
passaggio3 = models.CharField(max_length=50, blank=True)
passaggio4 = models.CharField(max_length=50, blank=True)
passaggio5 = models.CharField(max_length=50, blank=True)
consegna = (
('C', 'Consegnato'),
('N', 'Non consegnato'),
)
consegnato = models.CharField(max_length=1, choices=consegna)

#consegnato = models.BooleanField(blank=True)
#esito = models.CharField(max_length=10, blank=True)
flag = models.CharField(max_length=2, blank=True)
AnagraficaCliente = models.ForeignKey(AnagraficaCliente,
on_delete=models.CASCADE, related_name='trackings')

def __str__(self):
return self.track

class Meta:
verbose_name = "Tracking"
verbose_name_plural = "Trackings"


I make a script that read from trackingtest.csv and store in result_str_csv_anag_index_8_list then read from anagraficatest.csv and insert in mysql, after this,i get a query from table tracking_anagraficacliente (
previously entered in mysql previously) and i get last field data to store in result_str_query_anag_index_8_list then compare result_str_csv_anag_index_8_list in result_str_query_anag_index_8_list, if foreign key is correct.



All of this work with a cron in linux but i'd like to implement inside django (in backend) with alert, (for example button red or green if the insert work fine).



I hope to explain well what I have done, i'm beginner ;)



Thank you.



#!/usr/bin/python3

import csv
import MySQLdb, time

sql_anag= """
INSERT INTO tracking_anagraficacliente
(codice_cliente,nome,cognome,ragione_sociale,indirizzo,cap,piva,vatnumber,ragione_sociale_dest,indirizzo_dest)
VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
-- no primary key aggiornata

nome = VALUES(nome),
cognome = VALUES(cognome),
ragione_sociale = VALUES(ragione_sociale),
indirizzo = VALUES(indirizzo),
cap = VALUES(cap),
piva = VALUES(piva),
vatnumber = VALUES(vatnumber),
ragione_sociale_dest = VALUES(ragione_sociale_dest),
indirizzo_dest = VALUES(indirizzo_dest);
"""
sql_track= """
INSERT INTO tracking_tracking

VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
-- no primary key aggiornata

passaggio1 = VALUES(passaggio1),
passaggio2 = VALUES(passaggio2),
passaggio3 = VALUES(passaggio3),
passaggio4 = VALUES(passaggio4),
passaggio5 = VALUES(passaggio5),
consegnato = VALUES(consegnato),
flag = VALUES(flag),
AnagraficaCliente_id = VALUES(AnagraficaCliente_id);
"""
query_id_anag = """ select codice_cliente from tracking_anagraficacliente; """
#query_id_track = """ select AnagraficaCliente_id from tracking_tracking; """


db = MySQLdb.connect(
host="localhost", # your host
user="xxxxxxx", # your username
passwd="xxxxxx", # your password
db="lm_cms") # name of the data base

cur = db.cursor()

with open('trackingtest.csv') as csvfile: #
readCSVQuery = csv.reader(csvfile, delimiter=',')
next (readCSVQuery)
list_csv_tracking =
for i in readCSVQuery:
trackAnagraficaId = i[8]
list_csv_tracking.append(trackAnagraficaId)
result_str_csv_track_index_8 = str(list_csv_tracking).replace(",",
"").replace("[", "").replace("]", "")
result_str_csv_anag_index_8_list = result_str_csv_track_index_8.split(' ')
print ("Risultato CSV Tracking Foreign
Key",result_str_csv_anag_index_8_list)

def insert_anagrafica():
with open('anagraficatest.csv') as csvfile: #read CSV anagraficatest
readCSV = csv.reader(csvfile, delimiter=',')
next (readCSV) # Jump first line
for row_anag in readCSV:
try:
cur.execute (sql_anag, (row_anag)) #Insert data on mysql
db.commit() # Commit your changes in the database
print(row_anag)
except (MySQLdb.Error, MySQLdb.Warning) as e: #Exception
print(e)
db.rollback() # Rollback in case there is any error
db.close()

def query_anag():
cur.execute(query_id_anag) #Execute query to get codice_cliente from tracking_anagraficacliente
results_query_id_anag = cur.fetchall() # Get all data from query and assign to results_query_id_anag
result_query_anag = str(results_query_id_anag).replace("(", "").replace(")", "").replace(",", "")# Delete ( ) , from results_query_id_anag and convert to string
result_str_query_anag_index_8_list = result_query_anag.split(' ') # Convert in string from result_query_anag to result_str_query_anag_index_8_list
print ("Risultato Query anagrafica Codice cliente ",result_str_query_anag_index_8_list)
return result_str_query_anag_index_8_list #return value

def insert_tracking():
with open('trackingtest.csv') as csvfile: #Read Csv
readCSV = csv.reader(csvfile, delimiter=',') #Read csv file
next (readCSV) #Jump first line
#print (results_query_id_anag)
#compare if AnagraficaClienteID in trackingtest.csv is present in mysql tracking_aanagraficacliente Codice Cliente
#,if present make the insert sql_track else no
for i in result_str_csv_anag_index_8_list: #compare element lists in result_str_csv_anag_index_8_list is present in result_str_query_anag_index_8_list
if i in result_str_query_anag_index_8_list:
for row_track in readCSV: # Read all the line in trackingtest.csv
try:
#print(row_track[0])
#print(row_track)

cur.execute (sql_track, (row_track)) #Insert data trackingtest.csv in tracking_tracking
db.commit() # Commit your changes in the database
print ("Insert effettuata con successo ", row_track)
except (MySQLdb.Error, MySQLdb.Warning) as e:
print (e, "Riga non inserita: ",row_track)
#db.close() # disconnect from server
else:
print ("Attenzione, una o più righe non sono state inserite")


insert_anagrafica()
time.sleep(2)
(result_str_query_anag_index_8_list) = query_anag() #value passed from def query_anag() to insert_tracking
time.sleep(2)
insert_tracking()









share|improve this question

























  • when you say gettting csv automatically, how? ftp? local file? http/s? also, can you give example of code you tried? a sample of the csv to be imported? is this an insert and replace or an upsert (update/insert)?

    – Incognos
    Nov 24 '18 at 8:54











  • I thought update db through an ftp server, all data that uploaded on ftp with upsert command. My question is, whatis the best practise? Through script in python that update and insert on mysql, (with an cron job) or there is anothe solutions with django? Thank you.

    – Marcello Colangelo
    Nov 25 '18 at 12:34













  • You are collecting the file from a ftp server (hopefully sftp) then opening said file and running through the db to see if the record exists, then update and if not insert? cron is the simplest and least bulletproof way to implement. would recommend using cronitor if you go down that route

    – Incognos
    Nov 25 '18 at 12:37











  • could you please post your code? we cannot write all the code for you from scratch? you have to show us what you attempted so far.

    – Incognos
    Nov 25 '18 at 12:37











  • no, no i'm sorry i don't want that you write form me my code, i ask the best way to do, in this week i post the code. Thank you.

    – Marcello Colangelo
    Nov 25 '18 at 14:22














0












0








0








i'm Marcello from italy, i'm new in python and django. I ask a question to get info for my project in django.



I've created a project that have two tables, my problem is that how get data automatically from csv, i will create an ftp server that stored csv.txt and update the tables (for example every day).



Now i update with Django import / export manually. I would like that django read csv files and update db then delete.



Thank you very much for all your support!!!



my models.py:



from django.db import models
from django.urls import reverse



class AnagraficaCliente(models.Model):
codice_cliente = models.CharField(max_length=20, primary_key=True,
null=False, unique=True)
ragione_sociale = models.CharField(max_length=40)
ragione_sociale_dest = models.CharField(max_length=40)
nome = models.CharField(max_length=20, blank=True)
cognome = models.CharField(max_length=20, blank=True)
ragione_sociale = models.CharField(max_length=20)
indirizzo = models.TextField(blank=True)
cap = models.CharField(max_length=5, blank=True)
piva = models.CharField(max_length=11, blank=True)
vatnumber = models.CharField(max_length=13, blank=True)
ragione_sociale_dest = models.CharField(max_length=40)
indirizzo_dest = models.TextField(null=True)

def __str__(self):
#return self.ragione_sociale + " " + self.codice_cliente
return self.ragione_sociale_dest + " " + self.indirizzo_dest

class Meta:
verbose_name = "AnagraficaCliente"
verbose_name_plural = "AnagraficaClienti"




class Tracking(models.Model):
track = models.CharField(max_length=10, null=False, unique=True,
primary_key=True)
passaggio1 = models.CharField(max_length=50, blank=True)
passaggio2 = models.CharField(max_length=50, blank=True)
passaggio3 = models.CharField(max_length=50, blank=True)
passaggio4 = models.CharField(max_length=50, blank=True)
passaggio5 = models.CharField(max_length=50, blank=True)
consegna = (
('C', 'Consegnato'),
('N', 'Non consegnato'),
)
consegnato = models.CharField(max_length=1, choices=consegna)

#consegnato = models.BooleanField(blank=True)
#esito = models.CharField(max_length=10, blank=True)
flag = models.CharField(max_length=2, blank=True)
AnagraficaCliente = models.ForeignKey(AnagraficaCliente,
on_delete=models.CASCADE, related_name='trackings')

def __str__(self):
return self.track

class Meta:
verbose_name = "Tracking"
verbose_name_plural = "Trackings"


I make a script that read from trackingtest.csv and store in result_str_csv_anag_index_8_list then read from anagraficatest.csv and insert in mysql, after this,i get a query from table tracking_anagraficacliente (
previously entered in mysql previously) and i get last field data to store in result_str_query_anag_index_8_list then compare result_str_csv_anag_index_8_list in result_str_query_anag_index_8_list, if foreign key is correct.



All of this work with a cron in linux but i'd like to implement inside django (in backend) with alert, (for example button red or green if the insert work fine).



I hope to explain well what I have done, i'm beginner ;)



Thank you.



#!/usr/bin/python3

import csv
import MySQLdb, time

sql_anag= """
INSERT INTO tracking_anagraficacliente
(codice_cliente,nome,cognome,ragione_sociale,indirizzo,cap,piva,vatnumber,ragione_sociale_dest,indirizzo_dest)
VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
-- no primary key aggiornata

nome = VALUES(nome),
cognome = VALUES(cognome),
ragione_sociale = VALUES(ragione_sociale),
indirizzo = VALUES(indirizzo),
cap = VALUES(cap),
piva = VALUES(piva),
vatnumber = VALUES(vatnumber),
ragione_sociale_dest = VALUES(ragione_sociale_dest),
indirizzo_dest = VALUES(indirizzo_dest);
"""
sql_track= """
INSERT INTO tracking_tracking

VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
-- no primary key aggiornata

passaggio1 = VALUES(passaggio1),
passaggio2 = VALUES(passaggio2),
passaggio3 = VALUES(passaggio3),
passaggio4 = VALUES(passaggio4),
passaggio5 = VALUES(passaggio5),
consegnato = VALUES(consegnato),
flag = VALUES(flag),
AnagraficaCliente_id = VALUES(AnagraficaCliente_id);
"""
query_id_anag = """ select codice_cliente from tracking_anagraficacliente; """
#query_id_track = """ select AnagraficaCliente_id from tracking_tracking; """


db = MySQLdb.connect(
host="localhost", # your host
user="xxxxxxx", # your username
passwd="xxxxxx", # your password
db="lm_cms") # name of the data base

cur = db.cursor()

with open('trackingtest.csv') as csvfile: #
readCSVQuery = csv.reader(csvfile, delimiter=',')
next (readCSVQuery)
list_csv_tracking =
for i in readCSVQuery:
trackAnagraficaId = i[8]
list_csv_tracking.append(trackAnagraficaId)
result_str_csv_track_index_8 = str(list_csv_tracking).replace(",",
"").replace("[", "").replace("]", "")
result_str_csv_anag_index_8_list = result_str_csv_track_index_8.split(' ')
print ("Risultato CSV Tracking Foreign
Key",result_str_csv_anag_index_8_list)

def insert_anagrafica():
with open('anagraficatest.csv') as csvfile: #read CSV anagraficatest
readCSV = csv.reader(csvfile, delimiter=',')
next (readCSV) # Jump first line
for row_anag in readCSV:
try:
cur.execute (sql_anag, (row_anag)) #Insert data on mysql
db.commit() # Commit your changes in the database
print(row_anag)
except (MySQLdb.Error, MySQLdb.Warning) as e: #Exception
print(e)
db.rollback() # Rollback in case there is any error
db.close()

def query_anag():
cur.execute(query_id_anag) #Execute query to get codice_cliente from tracking_anagraficacliente
results_query_id_anag = cur.fetchall() # Get all data from query and assign to results_query_id_anag
result_query_anag = str(results_query_id_anag).replace("(", "").replace(")", "").replace(",", "")# Delete ( ) , from results_query_id_anag and convert to string
result_str_query_anag_index_8_list = result_query_anag.split(' ') # Convert in string from result_query_anag to result_str_query_anag_index_8_list
print ("Risultato Query anagrafica Codice cliente ",result_str_query_anag_index_8_list)
return result_str_query_anag_index_8_list #return value

def insert_tracking():
with open('trackingtest.csv') as csvfile: #Read Csv
readCSV = csv.reader(csvfile, delimiter=',') #Read csv file
next (readCSV) #Jump first line
#print (results_query_id_anag)
#compare if AnagraficaClienteID in trackingtest.csv is present in mysql tracking_aanagraficacliente Codice Cliente
#,if present make the insert sql_track else no
for i in result_str_csv_anag_index_8_list: #compare element lists in result_str_csv_anag_index_8_list is present in result_str_query_anag_index_8_list
if i in result_str_query_anag_index_8_list:
for row_track in readCSV: # Read all the line in trackingtest.csv
try:
#print(row_track[0])
#print(row_track)

cur.execute (sql_track, (row_track)) #Insert data trackingtest.csv in tracking_tracking
db.commit() # Commit your changes in the database
print ("Insert effettuata con successo ", row_track)
except (MySQLdb.Error, MySQLdb.Warning) as e:
print (e, "Riga non inserita: ",row_track)
#db.close() # disconnect from server
else:
print ("Attenzione, una o più righe non sono state inserite")


insert_anagrafica()
time.sleep(2)
(result_str_query_anag_index_8_list) = query_anag() #value passed from def query_anag() to insert_tracking
time.sleep(2)
insert_tracking()









share|improve this question
















i'm Marcello from italy, i'm new in python and django. I ask a question to get info for my project in django.



I've created a project that have two tables, my problem is that how get data automatically from csv, i will create an ftp server that stored csv.txt and update the tables (for example every day).



Now i update with Django import / export manually. I would like that django read csv files and update db then delete.



Thank you very much for all your support!!!



my models.py:



from django.db import models
from django.urls import reverse



class AnagraficaCliente(models.Model):
codice_cliente = models.CharField(max_length=20, primary_key=True,
null=False, unique=True)
ragione_sociale = models.CharField(max_length=40)
ragione_sociale_dest = models.CharField(max_length=40)
nome = models.CharField(max_length=20, blank=True)
cognome = models.CharField(max_length=20, blank=True)
ragione_sociale = models.CharField(max_length=20)
indirizzo = models.TextField(blank=True)
cap = models.CharField(max_length=5, blank=True)
piva = models.CharField(max_length=11, blank=True)
vatnumber = models.CharField(max_length=13, blank=True)
ragione_sociale_dest = models.CharField(max_length=40)
indirizzo_dest = models.TextField(null=True)

def __str__(self):
#return self.ragione_sociale + " " + self.codice_cliente
return self.ragione_sociale_dest + " " + self.indirizzo_dest

class Meta:
verbose_name = "AnagraficaCliente"
verbose_name_plural = "AnagraficaClienti"




class Tracking(models.Model):
track = models.CharField(max_length=10, null=False, unique=True,
primary_key=True)
passaggio1 = models.CharField(max_length=50, blank=True)
passaggio2 = models.CharField(max_length=50, blank=True)
passaggio3 = models.CharField(max_length=50, blank=True)
passaggio4 = models.CharField(max_length=50, blank=True)
passaggio5 = models.CharField(max_length=50, blank=True)
consegna = (
('C', 'Consegnato'),
('N', 'Non consegnato'),
)
consegnato = models.CharField(max_length=1, choices=consegna)

#consegnato = models.BooleanField(blank=True)
#esito = models.CharField(max_length=10, blank=True)
flag = models.CharField(max_length=2, blank=True)
AnagraficaCliente = models.ForeignKey(AnagraficaCliente,
on_delete=models.CASCADE, related_name='trackings')

def __str__(self):
return self.track

class Meta:
verbose_name = "Tracking"
verbose_name_plural = "Trackings"


I make a script that read from trackingtest.csv and store in result_str_csv_anag_index_8_list then read from anagraficatest.csv and insert in mysql, after this,i get a query from table tracking_anagraficacliente (
previously entered in mysql previously) and i get last field data to store in result_str_query_anag_index_8_list then compare result_str_csv_anag_index_8_list in result_str_query_anag_index_8_list, if foreign key is correct.



All of this work with a cron in linux but i'd like to implement inside django (in backend) with alert, (for example button red or green if the insert work fine).



I hope to explain well what I have done, i'm beginner ;)



Thank you.



#!/usr/bin/python3

import csv
import MySQLdb, time

sql_anag= """
INSERT INTO tracking_anagraficacliente
(codice_cliente,nome,cognome,ragione_sociale,indirizzo,cap,piva,vatnumber,ragione_sociale_dest,indirizzo_dest)
VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
-- no primary key aggiornata

nome = VALUES(nome),
cognome = VALUES(cognome),
ragione_sociale = VALUES(ragione_sociale),
indirizzo = VALUES(indirizzo),
cap = VALUES(cap),
piva = VALUES(piva),
vatnumber = VALUES(vatnumber),
ragione_sociale_dest = VALUES(ragione_sociale_dest),
indirizzo_dest = VALUES(indirizzo_dest);
"""
sql_track= """
INSERT INTO tracking_tracking

VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
-- no primary key aggiornata

passaggio1 = VALUES(passaggio1),
passaggio2 = VALUES(passaggio2),
passaggio3 = VALUES(passaggio3),
passaggio4 = VALUES(passaggio4),
passaggio5 = VALUES(passaggio5),
consegnato = VALUES(consegnato),
flag = VALUES(flag),
AnagraficaCliente_id = VALUES(AnagraficaCliente_id);
"""
query_id_anag = """ select codice_cliente from tracking_anagraficacliente; """
#query_id_track = """ select AnagraficaCliente_id from tracking_tracking; """


db = MySQLdb.connect(
host="localhost", # your host
user="xxxxxxx", # your username
passwd="xxxxxx", # your password
db="lm_cms") # name of the data base

cur = db.cursor()

with open('trackingtest.csv') as csvfile: #
readCSVQuery = csv.reader(csvfile, delimiter=',')
next (readCSVQuery)
list_csv_tracking =
for i in readCSVQuery:
trackAnagraficaId = i[8]
list_csv_tracking.append(trackAnagraficaId)
result_str_csv_track_index_8 = str(list_csv_tracking).replace(",",
"").replace("[", "").replace("]", "")
result_str_csv_anag_index_8_list = result_str_csv_track_index_8.split(' ')
print ("Risultato CSV Tracking Foreign
Key",result_str_csv_anag_index_8_list)

def insert_anagrafica():
with open('anagraficatest.csv') as csvfile: #read CSV anagraficatest
readCSV = csv.reader(csvfile, delimiter=',')
next (readCSV) # Jump first line
for row_anag in readCSV:
try:
cur.execute (sql_anag, (row_anag)) #Insert data on mysql
db.commit() # Commit your changes in the database
print(row_anag)
except (MySQLdb.Error, MySQLdb.Warning) as e: #Exception
print(e)
db.rollback() # Rollback in case there is any error
db.close()

def query_anag():
cur.execute(query_id_anag) #Execute query to get codice_cliente from tracking_anagraficacliente
results_query_id_anag = cur.fetchall() # Get all data from query and assign to results_query_id_anag
result_query_anag = str(results_query_id_anag).replace("(", "").replace(")", "").replace(",", "")# Delete ( ) , from results_query_id_anag and convert to string
result_str_query_anag_index_8_list = result_query_anag.split(' ') # Convert in string from result_query_anag to result_str_query_anag_index_8_list
print ("Risultato Query anagrafica Codice cliente ",result_str_query_anag_index_8_list)
return result_str_query_anag_index_8_list #return value

def insert_tracking():
with open('trackingtest.csv') as csvfile: #Read Csv
readCSV = csv.reader(csvfile, delimiter=',') #Read csv file
next (readCSV) #Jump first line
#print (results_query_id_anag)
#compare if AnagraficaClienteID in trackingtest.csv is present in mysql tracking_aanagraficacliente Codice Cliente
#,if present make the insert sql_track else no
for i in result_str_csv_anag_index_8_list: #compare element lists in result_str_csv_anag_index_8_list is present in result_str_query_anag_index_8_list
if i in result_str_query_anag_index_8_list:
for row_track in readCSV: # Read all the line in trackingtest.csv
try:
#print(row_track[0])
#print(row_track)

cur.execute (sql_track, (row_track)) #Insert data trackingtest.csv in tracking_tracking
db.commit() # Commit your changes in the database
print ("Insert effettuata con successo ", row_track)
except (MySQLdb.Error, MySQLdb.Warning) as e:
print (e, "Riga non inserita: ",row_track)
#db.close() # disconnect from server
else:
print ("Attenzione, una o più righe non sono state inserite")


insert_anagrafica()
time.sleep(2)
(result_str_query_anag_index_8_list) = query_anag() #value passed from def query_anag() to insert_tracking
time.sleep(2)
insert_tracking()






python mysql django csv django-models






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 18 '18 at 20:13







Marcello Colangelo

















asked Nov 24 '18 at 8:46









Marcello ColangeloMarcello Colangelo

11




11













  • when you say gettting csv automatically, how? ftp? local file? http/s? also, can you give example of code you tried? a sample of the csv to be imported? is this an insert and replace or an upsert (update/insert)?

    – Incognos
    Nov 24 '18 at 8:54











  • I thought update db through an ftp server, all data that uploaded on ftp with upsert command. My question is, whatis the best practise? Through script in python that update and insert on mysql, (with an cron job) or there is anothe solutions with django? Thank you.

    – Marcello Colangelo
    Nov 25 '18 at 12:34













  • You are collecting the file from a ftp server (hopefully sftp) then opening said file and running through the db to see if the record exists, then update and if not insert? cron is the simplest and least bulletproof way to implement. would recommend using cronitor if you go down that route

    – Incognos
    Nov 25 '18 at 12:37











  • could you please post your code? we cannot write all the code for you from scratch? you have to show us what you attempted so far.

    – Incognos
    Nov 25 '18 at 12:37











  • no, no i'm sorry i don't want that you write form me my code, i ask the best way to do, in this week i post the code. Thank you.

    – Marcello Colangelo
    Nov 25 '18 at 14:22



















  • when you say gettting csv automatically, how? ftp? local file? http/s? also, can you give example of code you tried? a sample of the csv to be imported? is this an insert and replace or an upsert (update/insert)?

    – Incognos
    Nov 24 '18 at 8:54











  • I thought update db through an ftp server, all data that uploaded on ftp with upsert command. My question is, whatis the best practise? Through script in python that update and insert on mysql, (with an cron job) or there is anothe solutions with django? Thank you.

    – Marcello Colangelo
    Nov 25 '18 at 12:34













  • You are collecting the file from a ftp server (hopefully sftp) then opening said file and running through the db to see if the record exists, then update and if not insert? cron is the simplest and least bulletproof way to implement. would recommend using cronitor if you go down that route

    – Incognos
    Nov 25 '18 at 12:37











  • could you please post your code? we cannot write all the code for you from scratch? you have to show us what you attempted so far.

    – Incognos
    Nov 25 '18 at 12:37











  • no, no i'm sorry i don't want that you write form me my code, i ask the best way to do, in this week i post the code. Thank you.

    – Marcello Colangelo
    Nov 25 '18 at 14:22

















when you say gettting csv automatically, how? ftp? local file? http/s? also, can you give example of code you tried? a sample of the csv to be imported? is this an insert and replace or an upsert (update/insert)?

– Incognos
Nov 24 '18 at 8:54





when you say gettting csv automatically, how? ftp? local file? http/s? also, can you give example of code you tried? a sample of the csv to be imported? is this an insert and replace or an upsert (update/insert)?

– Incognos
Nov 24 '18 at 8:54













I thought update db through an ftp server, all data that uploaded on ftp with upsert command. My question is, whatis the best practise? Through script in python that update and insert on mysql, (with an cron job) or there is anothe solutions with django? Thank you.

– Marcello Colangelo
Nov 25 '18 at 12:34







I thought update db through an ftp server, all data that uploaded on ftp with upsert command. My question is, whatis the best practise? Through script in python that update and insert on mysql, (with an cron job) or there is anothe solutions with django? Thank you.

– Marcello Colangelo
Nov 25 '18 at 12:34















You are collecting the file from a ftp server (hopefully sftp) then opening said file and running through the db to see if the record exists, then update and if not insert? cron is the simplest and least bulletproof way to implement. would recommend using cronitor if you go down that route

– Incognos
Nov 25 '18 at 12:37





You are collecting the file from a ftp server (hopefully sftp) then opening said file and running through the db to see if the record exists, then update and if not insert? cron is the simplest and least bulletproof way to implement. would recommend using cronitor if you go down that route

– Incognos
Nov 25 '18 at 12:37













could you please post your code? we cannot write all the code for you from scratch? you have to show us what you attempted so far.

– Incognos
Nov 25 '18 at 12:37





could you please post your code? we cannot write all the code for you from scratch? you have to show us what you attempted so far.

– Incognos
Nov 25 '18 at 12:37













no, no i'm sorry i don't want that you write form me my code, i ask the best way to do, in this week i post the code. Thank you.

– Marcello Colangelo
Nov 25 '18 at 14:22





no, no i'm sorry i don't want that you write form me my code, i ask the best way to do, in this week i post the code. Thank you.

– Marcello Colangelo
Nov 25 '18 at 14:22












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456589%2fdjango-csv-import-automatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456589%2fdjango-csv-import-automatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)