Saturday, April 16, 2022

TypeError: Object of type datetime is not JSON serializable

 

This error "TypeError: Object of type datetime is not JSON serializable" happens when we try to convert data with date time into json dictionary , so to fix this error we have to convert the data to string first then put it in the dictionary and then we can convert the dictionary to json format
...
for example if we want to create api with odoo to return the data for sale order , so we can put the sale order data as bellow 

order_data = {
            'id':order_id.id,
            'name':order_id.name,
            'customer':{'id':order_id.partner_id.id,'name':order_id.partner_id.name},
            'salesperson':{'id':order_id.user_id.id,'name':order_id.user_id.name},
            'date_order':str(order_id.date_order),
            'state':order_id.state,
            'products':order_products,
            'untaxed_amount':order_id.amount_untaxed,
            'total_tax': order_id.amount_tax,
            'total_amount':order_id.amount_total,
     }

return Response(json.dumps(order_data),headers=headers)

No comments:

Post a Comment

Odoo Invoice Qr code issues

There are two main issues must of us facing with the QR code in Odoo invoice & these issues are 1/ QR code displayed as broken image w...