I'm using Cloud9 (which from what I've read uses Python 2.6 currently, not Python 3) to write a Django app. I'm trying to read a CSV file with DictReader and use each column in the CSV to create a new instance of a model and populate the model fields.
views.py
class GenerateFromCSV(CreateView):
model = group
template_name = "my_app/csv_generate.html"
def form_valid(self, form):
new_group = form.save()
the_csv = open(new_group.group_csv, 'rbU')
fieldnames = ['c_type', 'f_name', 'q_type', 'ans', 'n_questions', 'bucket']
data_file = csv.DictReader(the_csv, fieldnames = fieldnames, delimiter=',', dialect=csv.excel)
for row in data_file:
new_card = Card(
name = 'card',
card_type = row['c_type'],
file_name = row['f_name'],
question_type = row['q_type'],
answer = row['ans'],
num_questions = row['n_questions'],
bucket = row['bucket'],
exam = new_exam)
new_card.save()
models.py
class Group(models.Model):
name = models.CharField(max_length=255, blank = True)
subject = models.CharField(max_length=255, choices = SUBJECT, blank = True)
num_questions = models.IntegerField(default=0, blank = True, null = True)
group_csv = models.FileField(upload_to='csv', blank = True, null = True)
def __unicode__(self):
return self.name
class Card(models.Model):
name = models.CharField(max_length=255, blank = True)
#ordered same as column order in CSV
card_type = models.CharField(max_length=255, choices = CARDTYPE, blank = True)
file_name = models.CharField(max_length=255, blank = True)
question_type = models.IntegerField(default = 0, blank = True, null = True)
answer = models.IntegerField(max_length = 1, choices = ANSWERS, blank = True, null = True)
num_questions = models.IntegerField(default = 0, blank = True, null = True)
bucket = models.CharField(max_length=255, blank = True)
exam = models.ForeignKey(Exam)
def __unicode__(self):
return self.name or 'card'
With the code as it is above, I get a TypeError (coercing to Unicode: need string or buffer, FieldFile found) when I call open() on the CSV. If I remove the call to open(), I get the error: 'new-line character seen in unquoted field - do you need to open the file in universal-newline mode?'
My CSV is in the format (not every column contains data in every row):
3,the_file_name.png,0,"00001",,Equations
What is the correct syntax for this?
Edit Here's my stacktrace:
Traceback:
File "/usr/libexec/openshift/cartridges/c9-0.1/root/python2.6.6/site-packages/Django-1.5-py2.6.egg/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/libexec/openshift/cartridges/c9-0.1/root/python2.6.6/site-packages/Django-1.5-py2.6.egg/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/usr/libexec/openshift/cartridges/c9-0.1/root/python2.6.6/site-packages/Django-1.5-py2.6.egg/django/views/generic/base.py" in dispatch
86. return handler(request, *args, **kwargs)
File "/usr/libexec/openshift/cartridges/c9-0.1/root/python2.6.6/site-packages/Django-1.5-py2.6.egg/django/views/generic/edit.py" in post
199. return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/usr/libexec/openshift/cartridges/c9-0.1/root/python2.6.6/site-packages/Django-1.5-py2.6.egg/django/views/generic/edit.py" in post
165. return self.form_valid(form)
File "/var/lib/stickshift/52a55ef4e0b8cde0ff000036/app-root/data/705411/zamrdjango/zamr/views.py" in form_valid
35. with new_exam.exam_csv.open('rbU') as the_csv:
Exception Type: AttributeError at /import/
Exception Value: 'NoneType' object has no attribute '__exit__'
new_item = Item(did you meannew_card = Cardinstead ?