python - Adding data from csv file to database using django -




i have django model follows:

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) 

that model creates table in database id follows: enter image description here

now want add records csv file model. csv file follows:

enter image description here

i use copy command follows:

copy master_traxiorelations(corgemeente, coradres, corhuisnr) '/path/to/file.csv' csv header delimiter ';' encoding 'iso-8859-1'; 

and when execute it, error follows:

[2017-08-21 15:50:49] [22p04] error: data after last expected column 

but when add id copy command, copy master_traxiorelations(id, corgemeente, coradres, corhuisnr)

and csv file also:

enter image description here

then works fine.

how can add data file database, without adding id copy command , csv file?

i solved adding id model follows:

id = models.autofield(primary_key=true) 

adding id = models.autofield(primary_key=true) enables auto incrementation of id. model looks :

 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) 

and copy command works without adding id column in csv file.

copy master_traxiorelations(corgemeente, coradres, corhuisnr) '/path/to/file.csv' csv header delimiter ';' encoding 'iso-8859-1'; 

hope helps if else has same problem.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -