Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango csv error and best place to write business logic
    text
    copied!<p><strong>EDIT:</strong></p> <p>Q1 is solved by adding <code>enctype="multipart/form-data"</code> to the form template. Please comment on Q2.</p> <p><strong>Qn1:</strong> I get the below error when try to read a csv file, which is uploaded through Forms. Exception may be at this line.</p> <pre><code>records = csv.reader(f) </code></pre> <p>Not sure what to pass . Please see the code below.</p> <p><strong>Qn2:</strong> Is it ok to do the csv processing in the model (the csv is for uploading domain data, hence validate/and persist as domain objects). I am new to Django/Python, and most of the example I see so far has not much method in models. This is bit different from what I used to where you capture all the business logic associated with the model in it. Wonder the idiomatic usage in django.</p> <h2>Exception:</h2> <pre><code>Django Version: 1.4.1 Exception Type: TypeError Exception Value: argument 1 must be an iterator </code></pre> <h2>View:</h2> <pre><code>def upload(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): w = Testme() w.importCsv(form.cleaned_data["file"]) return HttpResponseRedirect('/') else: form = UploadFileForm() return render_to_response('setup.html', {'form': form},context_instance=RequestContext(request)) </code></pre> <h2>Form:</h2> <pre><code>class UploadFileForm(forms.Form): title = forms.CharField(required=False) file = forms.FileField(required=False) </code></pre> <h2>Model:</h2> <pre><code>class Testme(models.Model): code = models.IntegerField() ctu = models.IntegerField() address = UsAddress def importCsv(self, f): records = csv.reader(f) for line in records: logger.debug(line) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload