将从用户文件上传的CSV数据添加到Django模型

时间:2020-10-09 12:20:24

标签: reactjs django csv django-models django-rest-framework

很抱歉,如果这是一个愚蠢的问题,但是我找不到从用户在前端(React)更新的.csv文件中填充Django模型的简单/最新方法。我设法使用axios将上载的文件从react前端发送到后端,我可以在后端捕获文件,但是我不知道用csv数据填充模型的正确方法...

谢谢!

App.js

// frontend/src/App.js

import React, { Component } from "react";
import axios from "axios";
import { Button, FormGroup, Form } from "react-bootstrap";

class App extends Component {

  handleFile = (event) => {
    event.preventDefault();

    const fileToUpload = event.target.files[0];
    this.setState({
      fileToUpload: fileToUpload,
    });
  };

  handleSubmitData = (event) => {
    event.preventDefault();

    let formData = new FormData();
    formData.append("file", this.state.fileToUpload);

    axios
      .post("data/", formData)
      .then((response) => {
        console.log(response);
      })
      .catch((error) => {
        console.log(error.response);
      });
  };

  render() {
    return (
      <main className="content">
        <h1 className="text-white text-uppercase text-center my-4">
          Price comparison
        </h1>
        <form onSubmit={this.handleSubmitData}>
          <FormGroup>
            <Form.Label>Upload file</Form.Label>
            <Form.Control type="file" onChange={this.handleFile}></Form.Control>
          </FormGroup>

          <Button block bssize="large" type="submit">
            Submit
          </Button>
        </form>
      </main>
    );
  }
}
export default App;

models.py

from django.db import models    

class Data(models.Model):
    city = models.CharField(max_length=200, null=True)
    wheel_type = models.CharField(max_length=200, null=True)
    order_status = models.CharField(max_length=200, null=True)
    order_ID = models.IntegerField(null=True)
    client_ID = models.IntegerField(null=True)
    placed_time = models.DateTimeField(null=True)
    order_date_time = models.DateTimeField(null=True)
    origination_latitude = models.FloatField(null=True)
    origination_longitude = models.FloatField(null=True)
    destination_latitude = models.FloatField(null=True)
    destination_longitude = models.FloatField(null=True)
    surchage_total = models.FloatField(null=True)
    transaction_value = models.FloatField(null=True)
    total_price = models.FloatField(null=True)

    def __str__(self):
        return self.order_ID

views.py

from rest_framework.response import Response
from rest_framework.parsers import MultiPartParser, FormParser

class RegisterData(views.APIView):
    parser_classes = (FormParser, MultiPartParser)

    def post(self, request):
        for file in request.FILES.values():
            print(file)

        return Response({"success": "Good job, buddy"})

1 个答案:

答案 0 :(得分:1)

您可能想看一下批量创建方法:https://books.agiliq.com/projects/django-orm-cookbook/en/latest/multiple_objects.html

# not sure about the right import
from .models import Data

class RegisterData(views.APIView):
    parser_classes = (FormParser, MultiPartParser)

    def post(self, request):
        for file in request.FILES.values():
            reader = csv.reader(file)
            objects = []
            for row in reader:
                objects.append(Data(
                  city=row[0],
                  wheel_type=row[1],
                  # and so on ..
                ))
            Data.objects.bulk_create(objects)

        return Response({"success": "Good job, buddy"})

如果熊猫的CSV的第一行恰好是标题行,与模型中的字段值相同,那么您可以使用熊猫来做更奇特的选择,但我不建议仅以此为熊猫。