yousaf raza
yousaf raza

Reputation: 37

Importing csv in Django

i am trying to import csv file into db from manage.py shell but it is giving me error

import csv
with open(r"C:\Users\yousa\Desktop\xp\exp\Dist_Extended_TissuesWise_ClusteredAnnotated_21nov2018_qaz.csv", 'r') as f:
        reader = csv.reader(f)
        lines = list(reader)
        del lines[0]
        objects = []
        for line in lines:
            obj =Maize_clustert()
            obj.chromosome = int(line[0])
            obj.cluster_start = int(line[1])
            obj.cluster_end = int(line[2])
            obj.strand = line[3]
            obj.pac = int(line[4])
            obj.pac_suppoort = int(line[5])
            obj.cluster_support = int(line[6])
            obj.region = line[7]
            obj.gene_id = line[8]
            obj.transcript_id = line[9]
            obj.distance = line[10]
            obj.transcript_code = line[11]
            obj.gene_cord = line[12]
            obj.utr_length = int(line[13])
            obj.gene_biotype = line[14]
            obj.cluster_size = int(line[15])
            obj.number_pas = int(line[16])
            obj.zygote = int(line[17])
            obj.sperm = int(line[18])
            obj.egg = int(line[19])
            obj.root = int(line[20])
            obj.embryo = int(line[21])
            obj.basal = int(line[22])
            obj.ear = int(line[23])
            obj.apical = int(line[24])
            obj.ovule = int(line[24])
            objects.append(obj)
        Maize_clustert.objects.bulk_create(objects)

while runnig this code in manage.py shell it give me result

Traceback (most recent call last):
  File "<input>", line 8, in <module>
NameError: name 'Maize_clustert' is not defined

while in models.py i have created full model of my data is there any alternative way or i am doing it wrong kindly help me in it

Upvotes: 0

Views: 185

Answers (2)

ruddra
ruddra

Reputation: 51978

You need to import Maize_clustert in the file. Like:

# Please read the PEP-8 Style Guide on Naming convention
# Class Name should be 'PascalCase'
from yourapp.models import Maize_clustert

Upvotes: 1

Maciej M
Maciej M

Reputation: 786

Read info carefully:

NameError: name 'Maize_clustert' is not defined

That means that in file that you perform CSV reading this class is not defined. Thus even Maize_clustert exists in your models.py you have to import it in files that uses it.

Basically add this at top of your file:

`from application.models import Maize_clustert

Upvotes: 2

Related Questions