Import an xls or csv without Django model

0

I have been researching and can not find a way to import a file either xls or csv without having a model, I am trying to import files of this type into the database but they could vary in number of columns unexpectedly there is way to do what I need? I just need you to point me in the right direction

    
asked by Anthony 15.08.2017 в 15:12
source

1 answer

1

The correct way to save the file in the database is as follows:

class modelo(models.Model):
    titulo = models.CharField(max_length=50)
    archivo = models.FileField()

and then create a view where you capture the data of the sent form and save the file. It is necessary to clarify that this does not save the file in database, this uploads it to your "average" folder and saves in a database the reference to that file.

Once you have uploaded it, you can access it and read it in python normally (in a view).

You can also do it with a model that inherits from the Django forms:

from django import forms

class formulario_csv(forms.Form):
    titulo = forms.CharField(max_length=50)
    archivo = forms.FileField()

In this case it would be the same, solemente that when you save it you process it as a form. I leave the Django documentation to upload files: link

    
answered by 17.08.2017 / 17:52
source