繁体   English   中英

如何将 dataframe 添加到 django model (sqlite3)?

[英]How to add a dataframe to a django model (sqlite3)?

我有一个数据框,它有一个图像列和 rest 字符列,并填充了 128 行。 此 DataFrame 中的数据是被抓取的,需要不断的抓取才能始终拥有最新的记录。


mainline_t_shirt = pd.DataFrame({
    'Images': image,
    'Titles': title,
    'Prices': price,
    'link': link,
    'Website': 'mainlinemenswear',
    'brand': brand

// *not sure which file this needs to be added to
})

Django 中的 Model - /models.py

class T_shirt(models.Model):
    image = models.ImageField()
    title = models.CharField(max_length=250)
    price = models.CharField(max_length=250)
    link = models.CharField(max_length=250)
    website = models.CharField(max_length=250)
    brand = models.CharField(max_length=250)

谁能提供他们的专业知识来展示如何将数据从 DataFrame 移动到 Django 中的 sqlite3 数据库中? 我应该将这段代码添加到哪个文件?

我认为您不能简单地将这段代码添加到任何地方并使其工作。 但是下面的命令会将您的数据添加到特定的 model。

python manage.py loaddata fixtures.json

Json 文件应如下所示:

[{"fields": {'Images': image, 'Titles': title, 'Prices': price, 'link': link, 
'Website': 'mainlinemenswear','brand': brand}, "model":APP_NAME.T_shirt]

要到达那里,您可以遍历 DataFrame 并创建一个字典,以上述格式保存在 Json 中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM