thanks guys.i managed to complete it.million thanks again specially for DAVID,WM-EDDIE and S.LOTT.also STACKOVERFLOW
The solution:
**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)
the data in csv file:
abubakar,rooney,0178222123,student
abubakar,ronaldo,0183886789,student
abubakar,kaka,0197887898,bola
appreciated any suggestion.and hopefully can show me some coding example coz i'm still beginner in 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)