简体   繁体   中英

How to add a dataframe to a django model (sqlite3)?

I have a data frame that has one image column and the rest character columns and is populated with 128 rows. The data in this DataFrame is scraped and will need to be continuously scraped to always have the latest records.


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
})

Model in Django - /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)

Could anyone provide their expertise in showing how to move the data from the DataFrame into the sqlite3 database in Django? which file do I add this snippet of code to?

I don't think you could simply add this snippet of code anywhere and make it work. But the command below will add your data to a particular model.

python manage.py loaddata fixtures.json

The Json file should look like this:

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

To get there you could iterate through your DataFrame and create a dict to be saved in Json with the above format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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