Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
48 lines (44 sloc) 2.13 KB
#!/usr/bin/env python
#--------------------------------------------------------------------------------#
#Adding data to Children model from a csv file
#the csv file path should be changed based on the data in which file is stored
#--------------------------------------------------------------------------------#
from django.core.management import setup_environ
from django.contrib.auth.models import User
from django.core.validators import email_re
from django.db import IntegrityError
from learning_outcomes.models import Children
import csv
from learning import settings
setup_environ(settings)
#---------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------#
#--Opening the csv file and storing the data in variable 'c'--#
#---------------------------------------------------------------------------------#
c=[]
with open('/home/saad/learning/child_data.csv','rb') as f:
readers = csv.reader(f)
for row in readers:
c.append(row)
#--Traversing each line in c and each line will be added to the variable users------#
#--each line would be in format email,students name,current school, current test----#
#--values will be delimited by commas-----------------------------------------------#
for users in c:
email = users[0]
student=users[1]
current_school=users[2]
current_year=users[3]
current_test=users[4]
if email_re.match(email):
#--------------------------------------------------------------------------------------------------------#
#---------------In Users.py file email is split and the value before '@' is saved as username------------#
#---------------splitting email and storing it in tokens this is done to relate Child object with a user-#
#--------------------------------------------------------------------------------------------------------#
tokens = email.split('@')
username = tokens[0]
try:
user=User.objects.get(username=username)
child = Children.objects.create(parent=user,child=student,school=current_school,year=current_year,test=current_test)
child.save()
except IntegrityError:
print username,student