Jirka & Everybody

Back to basics is always a good advice. With your help and this guy:
http://www.beardygeek.com/2010/03/adding-views-to-the-django-admin/
I can create a form to upload csv files and input their records into
the database.

Short history:

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

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

This form was imported in a view:

@staff_member_required
def import(request):
    if request.method == "POST":
        form = DataInput(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            success = True
            context = {"form": form, "success": success}
            return render_to_response("imported.html", context,
            context_instance=RequestContext(request))
    else:
        form = DataInput()
        contexto = {"form": form}
        return render_to_response("imported.html", context,
            context_instance=RequestContext(request))

And add  the url pattern & some little changes to the admin index
template, and everything works the way i want.

Thanks guys!

On Oct 31, 6:01 am, Jirka Vejrazka <jirka.vejra...@gmail.com> wrote:
> > I try to follow the ideas, but i feel like taking the dirty way.
> > Here's my work:
> > Because i can't replace the modelform created by the admin for the
> > "Data" model with a standard form as the Django docs says, i created a
> > view to replace the "add" view generated by the admin and put a
> > standard form:
>
> Jorge,
>
>   I think you need to get back to basics and try to understand what is
> run where. All the code you are writing for Django is run on the
> server and only web pages are *displayed* to users on their machines.
> This means that in order to import a CSV file, you need to transfer it
> to the server first and then you'll be able to import it there.
>
>   Note that this has nothing to do with the admin site. This is a
> fundamental think to understand.
>
>   I'd suggest that you forget the admin site for the moment and focus
> on the "transfer CSV file to server first". Take a look at the email
> Felix has sent you regarding FileField and try to code the part that
> handles file upload to the server. Once you have that, you can start
> thinking about importing the contents of the file into the database.
>
>   Cheers
>
>     Jirka
>
> P.S. OK, there is a way to import data without uploading them to
> server first, but it's much more complicated than the one outlined
> above and involves multiple different technologies that have to work
> together.

-- 
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