Import csv file into mysql database using django web app
Thanks guys. I managed to fill it.million again thanks for DAVID, WM-EDDIE and S.LOTT.also STACKOVERFLOW
Decision:
**model = Contact() model.contact_owner = request.user model.contact_name = row[1] model.contact_mobile_no = row[2] model.select_group = row[3] model.save()**
my user.py
def import_contact(request):
if request.method == 'POST':
form = UploadContactForm(request.POST, request.FILES)
if form.is_valid():
csvfile = request.FILES['file']
print csvfile
csvfile.read()
testReader = csv.reader(csvfile,delimiter=' ', quotechar='|')
**#what code should i write here to store data in mysql**
for row in testReader:
print "|".join(row)
return HttpResponseRedirect('/admin')
else:
form = UploadContactForm()
vars = RequestContext(request, { 'form': form })
return render_to_response('admin/import_contact.html', vars)
data in csv file:
Abubakar, Rooney, 0178222123, student
Abubakar, Ronaldo, 0183886789, student
Abubakar, kaka, 0197887898, bola
appreciated any suggestion. And hopefully can show me a coz coding example. I am still new to this language.
my models.py:
class Contact(models.Model):
contact_owner = models.ForeignKey(User, related_name="contacts")
contact_name = models.CharField(max_length=20)
contact_mobile_no = models.CharField(max_length=20)
select_group = models.CharField(max_length=20, null=True)
def __unicode__(self):
return "contact {contact_owner=%s, contact_name=%s, contact_mobile_no=%s, select_group=%s}" % (self.contact_owner, self.contact_name, self.contact_mobile_no, self.select_group)
source to share
To tokenize a comma separated string:
>>> a = 'abubakar,rooney,0178222123,student abubakar,rooneyzzz,0178222164'
>>> b = a.split(',')
>>> print b
['abubakar', 'rooney', '0178222123', 'student abubakar', 'rooneyzzz', '0178222164']
See @ wm_eddie's answer for creating a new entry in your db.
source to share
Python instructions are pretty bad. You are supposed to understand that because they are using string.join () this string acts like a list. So pretty much it parses the CSV file in the list of lists. He takes care of all the nasty quotes and avoids you.
You use it in the same way as if you were using an array / list
for row in testReader:
model = YourModel()
model.property_a = row[0]
model.property_b = row[1]
model.save()
There is also a DictReader that allows you to write more readable:
for row in testReader:
model = YourModel()
model.property_a = row["prop_a"]
model.property_b = row["prop_b"]
model.save()
source to share
This is roughly what we are doing.
header = 'contact_owner', 'contact_name', 'contact_mobile_number', 'select_group'
rdr= csv.reader(request.FILES['file'])
for row in rdr:
data = zip( header, row )
owner, created = User.get_or_create( data['contact_owner']
data['contact_owner']= owner
contact, created = Contact.get_or_create( **data )
logger.info( "Created %r", contact )
http://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create-kwargs
Your question says "comma tokenizer". Your example data uses a comma. But your code is csv.reader
showing spaces. What he?
source to share