首页 > 解决方案 > 熊猫不导入 excel 更改

问题描述

我正在一个 python/django 项目中工作,我从 Excel 文件中导入一些数据。

我需要导入的简化视图是:

网址.py

re_path(r'^product_push/', sick_data_views.Products_List_upload, name='product-push')

视图.py

def ID_push():
    Products.objects.all().delete()
    df = product.importDataFrameProducts()
    print(df)
    data = Products(
                        ID = str(df['ID'].tolist()).replace("'",'\"'),
    )
    data.save()

def Products_List_upload(request):
    ID_push()
    return HttpResponse(status=204)

模型.py

class Products(models.Model):
    ID = models.CharField(max_length=1000000)

列表.py

class product:
    def __init__(self, excel_path = os.path.dirname(os.path.realpath(__file__))+"\\A_Products.xlsx"):
        self.__df =  self.importDataFrameExamplesList(excel_path)

    def importDataFrameExamplesList(self, excel_path):
        # Load json
        df = pd.read_excel(excel_path, keep_default_dates=False)
        return df

    def importDataFrameProducts(self):
        return self.__df

当我运行服务器时,导入过程运行并且我得到正确的输出:视图调用 lists.py 并更新值。比方说:

ID
1
2
3

但是,如果我将 Excel 文件更改为其他值,例如:

ID
4
2
3

如果我运行 localhost:8000/product_push,则导入返回 Excel 文件的旧值 (1,2,3)。我不需要自动更新,但是当我运行推送 url 时,我应该能够获取更新后的值。熊猫是否在缓存中存储了一些变量?因为我只有重新运行服务器后才能解决这个问题。提前致谢。

标签: pythondjangopandas

解决方案


推荐阅读