What is an efficient way of inserting thousands of records into an SQLite table using Django?

后端 未结 9 769
暖寄归人
暖寄归人 2020-11-30 17:41

I have to insert 8000+ records into a SQLite database using Django\'s ORM. This operation needs to be run as a cronjob about once per minute.
At the moment I\'m using a

9条回答
  •  温柔的废话
    2020-11-30 18:28

    def order(request):    
        if request.method=="GET":
            # get the value from html page
            cust_name = request.GET.get('cust_name', '')
            cust_cont = request.GET.get('cust_cont', '')
            pincode = request.GET.get('pincode', '')
            city_name = request.GET.get('city_name', '')
            state = request.GET.get('state', '')
            contry = request.GET.get('contry', '')
            gender = request.GET.get('gender', '')
            paid_amt = request.GET.get('paid_amt', '')
            due_amt = request.GET.get('due_amt', '')
            order_date = request.GET.get('order_date', '')
            prod_name = request.GET.getlist('prod_name[]', '')
            prod_qty = request.GET.getlist('prod_qty[]', '')
            prod_price = request.GET.getlist('prod_price[]', '')
    
            # insert customer information into customer table
            try:
                # Insert Data into customer table
                cust_tab = Customer(customer_name=cust_name, customer_contact=cust_cont, gender=gender, city_name=city_name, pincode=pincode, state_name=state, contry_name=contry)
                cust_tab.save()
                # Retrive Id from customer table
                custo_id = Customer.objects.values_list('customer_id').last()   #It is return Tuple as result from Queryset
                custo_id = int(custo_id[0]) #It is convert the Tuple in INT
                # Insert Data into Order table
                order_tab = Orders(order_date=order_date, paid_amt=paid_amt, due_amt=due_amt, customer_id=custo_id)
                order_tab.save()
                # Insert Data into Products table
                # insert multiple data at a one time from djanog using while loop
                i=0
                while(i

提交回复
热议问题