On Oct 28, 5:08 am, Jorge <aldea.diagu...@gmail.com> wrote:
> Shame on me!
> I forgot to make some changes inside the for loop.
> So now it is:
>
>  def save(self, commit=True, *args, **kwargs):
>          form_input = super(DataInput, self).save(commit=False, *args,
>  **kwargs)
>          form_input.place = self.cleaned_data['place']
>          form_input.file = self.cleaned_data['file']
>          records = csv.reader(form_input.file)
>          for line in records:
>              form_input.time = datetime.strptime(line[1], "%m/%d/%y %H:
> %M:
>  %S")
>              form_input.data_1 = line[2]
>              form_input.data_2 = line[3]
>              form_input.data_3 = line[4]
>              form_input.save()
>
> Buuuuut, i've got a new error message! an attribute error: 'NoneType'
> object has no attribute 'save'.
> What can it be?

Hang on, what you're doing here is repeatedly setting the data values
for each line to the *same* form_input model. Presumably what you
actually want to do is to create new Data instances for each line in
the CSV, with the place set to the value of the form.

In this case, I'd recommend not to bother with a ModelForm at all.
Instead, just use a standard Form, and create new Data instances in
your for loop:

class DataInput(forms.Form):
    file = forms.FileField()
    place = forms.ModelChoiceField(queryset=Places.objects.all())

    def save(self):
         records = csv.reader(self.cleaned_data[file])
         for line in records:
             data_obj = Data()
             data_obj.time = datetime.strptime(line[1],
                                       "%m/%d/%y %H: %M: %S")
             data_obj.data_1 = line[2]
             data_obj.data_2 = line[3]
             data_obj.data_3 = line[4]
             data_obj.save()

--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to