Django not saving file to FileField. IOError: File not open for reading

Django not saving file to FileField. IOError: File not open for reading

我有这个 Django 代码,我无法将文件保存到模型 FileField:

    # create stocklist object and populate it
    stocklist = StockList.objects.create(retailer=retailer)
    with open("retailer_stocklist.csv", "wb") as csv_file:
        for row in table_data:
            writer = csv.writer(csv_file, quoting=csv.QUOTE_ALL)
            writer.writerow([row["EAN"], row["NAME"], row["QUANTITY"],
                             row["UNIT"], row["SKU"], row["PRICE"]])

        stocklist.csv_file.save("retailer_stocklist.csv", File(csv_file))

模型看起来像这样:

class StockList(models.Model):
    csv_file = models.FileField(null=True, blank=True)

我收到错误:

IOError: File not open for reading

将文件模式从 wb 更改为 r+b