[Answered ]-Django: Import data from uploaded excel file

1👍

If you import your Dataset model you can create a new object based on the data you’ve already got from the file.

for example:

#import your model
from . models import Dataset


@login_required
def file_upload(request):
    data = None

    if request.method == 'POST':
        file_form = FileForm(request.POST, request.FILES)
        data_form = DatasetForm(request.POST, request.FILES)
        raw_file= request.FILES
        if file_form.is_valid() or data_form.is_valid():       
            data = request.FILES['file_upload']             
            data = pd.read_csv(data, header=0, encoding="UTF-8")                   
            first_row = data.iloc[[0]]
            second_row = data.iloc[[1]] 

            # create new dataset object
            Dataset.objects.create(
                name_user_A=first_row,
                name_user_B=second_row,
                ...,
                ...)

Leave a comment