Zkt22/z3/fruitapp-main/fruitapp-backend/seeder.py

41 lines
1.3 KiB
Python
Raw Normal View History

2022-05-22 10:40:18 +00:00
from main import Product, db
def insert_data(data):
product = Product.query.get(data['id'])
if not product:
product = Product(id=data['id'], title=data['title'], image=data['image'])
db.session.add(product)
db.session.commit()
print('inserted ', data['title'])
else:
print('Product already exists.')
PRODUCTS = [
{
'id': 1,
'title': 'Apple',
'image': 'https://media.istockphoto.com/photos/red-apple-picture-id184276818?k=20&m=184276818&s=612x612&w=0&h=QxOcueqAUVTdiJ7DVoCu-BkNCIuwliPEgtAQhgvBA_g='
},
{
'id': 2,
'title': 'Orange',
'image': 'https://media.istockphoto.com/photos/tiny-tangerine-picture-id89951356?k=20&m=89951356&s=170667a&w=0&h=9oW2EBTASfaBol8qYVYB8YNYcSoNe1AImG4wqdgErEs='
},
{
'id': 3,
'title': 'Banana',
'image': 'https://media.istockphoto.com/photos/banana-picture-id1184345169?k=20&m=1184345169&s=170667a&w=0&h=t22KqOZ9EEwyRj7i35uxY-Xf6P_gAgLejd-SeReTnPY='
},
{
'id': 4,
'title': 'Avocado',
'image': 'https://media.istockphoto.com/photos/half-of-fresh-ripe-avocado-isolated-on-white-background-picture-id1278032327?b=1&k=20&m=1278032327&s=170667a&w=0&h=7eMAI9S_YBiVaDN49zk5gUZEhhA1oGDIWh3Tdst6q4Y='
}
]
for p in PRODUCTS:
insert_data(p)