Reputation: 13
I have this class
class Order(db.Model):
__tablename__ = "orders"
id=db.Column(db.String, primary_key=True)
order_id = db.Column(db.String, nullable=True)
product_id=db.Column(db.String, nullable=True)
quantity=db.Column(db.Integer, nullable=True)
def save_to_db(self):
db.session.add_all(self)
db.session.commit()
And i have this class
class checkout(Resource):
# @jwt_required
def post(self):
# current_user = get_jwt_identity()
# user_wallet=Wallet.find_by_customer_id(current_user)
order_id=uuid.uuid1()
data = parser_checkout.parse_args()
print (data['Products'][0]['Product_id'])
mainlist=list()
for i in range(0,len(data['Products'])):
new_order_prod=Order(
id=uuid.uuid1(),
order_id=order_id,
product_id=data['Products'][i]['Product_id'],
quantity=data['Products'][i]['quantity']
)
mainlist.append(new_order_prod)
try:
mainlist.save_to_db()
return {
'status':'succes',
'message':'sucess order placed'
},200
except:
return {
'status':'error',
'message':'order placing failed'
},401
I m not getting any error but i found that mainlist.save_to_db()
is not working i mean that its not going inside the function of that class. Can anyone help me ?
I think its not entering that function becoz class object is tuple with concerned fields but i m making list and calling that so is that why its not working ? Any solution to this?
Upvotes: 0
Views: 633
Reputation: 4102
You're calling mainlist.save_to_db()
but you declare mainlist = list()
so it won't have a save_to_db
method. On top of that, you have a bare except
so any errors raised by doing that would be suppressed. It's best to only handle specific exceptions, or at least to log what the exceptions is if you're doing a catch-all except
.
Rather than putting a save_to_db
method on the Order
class, which means that calling it only affects 1 instance, call db.session.add_all(mainlist)
and db.session.commit()
directly, instead.
Upvotes: 1