Tuesday, February 24, 2015

Re: Read a csv file and save data in postgresql

Looking at your code, maybe this one is better

https://pypi.python.org/pypi/unicodecsv/0.9.4

This thing will make a dict, which you can make into a object by feeding it to a modelform.

K


On Tuesday, February 24, 2015 at 3:53:19 PM UTC-8, elcaiaimar wrote:
Hello,

I have a website and I need that users can introduce a csv file and save their data in a postgresql database. I can introduce a file and save it in media_dir and in the database (only the url). But what I want is save each row as a register in a table. I've searched some examples but I haven't found exactly this.

Is it possible to do? Has anyone done this before? I put my code to save the file in a database and in media below:

views
if 'formularioarchivo' in request.POST:
        formularioarchivo = UploadForm(request.POST, request.FILES)
        if formularioarchivo.is_valid():
          newdoc = Archivo(docfile = request.FILES['docfile'])
          newdoc.save(formularioarchivo)
          return HttpResponseRedirect('/edicioncuenca/')

        else:
        formularioarchivo = UploadForm()

models
class Archivo(models.Model):
    # gid_archivo = models.IntegerField(primary_key=True)
    docfile = models.FileField(upload_to='%Y/%m/%d')

forms
class UploadForm(forms.Form):
    docfile = forms.FileField(
        label='Selecciona un archivo'
    )

template
<form name="formulario" action="/edicioncuenca/" method="POST" enctype="multipart/form-data">
  {% csrf_token %}
  {{ formularioarchivo.as_p }}
  <input type="submit" name="formularioarchivo" value="Submit">
</form>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/73c23c31-f3b3-4064-bee5-45bad3e508cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment