0

I am trying to load the csv files I upload to my sqlite3 db. I am able to upload the csv into a folder but it doesn't appear on my db. The db table structure contains as many columns as the csv with the corresponding data types; but the column names in the db table structure are different as in the csv, as some of the columns names in the csv contain spaces and special characters (like accents for example).

My goal is to allow the user to upload a csv file that will be loaded to the db. I was also wondering if its possible to increment the db each time a new csv is loaded (for example: I upload a file now which is loaded to the db then the second time I add, the newer details are added to the db without deleting the previous ones). Apologies if I am unclear and asking too many things at once.

Here is my model, form, function (handle upload) & views .py files:

# models.py 

class UploadForm(models.Model):
    file = models.FileField()

    class Meta:
        db_table = "db_table"

# forms.py

class UploadForm(forms.ModelForm):
    class Meta:
        model = UploadForm
        fields = "__all__"
  
# functions.py

def handle_uploaded_file(f):
    with open('vps_app/static/upload/'+f.name, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

# views.py

def index(request):
    if request.method == 'POST':
        Uploaded = UploadForm(request.POST, request.FILES)
        if Uploaded.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponse("File uploaded successfuly")
    else:
        Uploaded = UploadForm()
        return render(request, "index.html", {'form': Uploaded})

Thanks in advance.

1 Answer 1

1
+50

You can't do this automatically, you need to handle it yourself, here is a sample for handle_uploaded_file

def handle_uploaded_file(f):
      for row in csv.DictReader(f):
             m = Model.objects.create(**row)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.