same code for create and update using save

I have a bunch of portions like this :

server_dict = {	
    'total_price': total_price,
    'total_price_usd': total_price_usd,
    'location_name': server.get("location", ""),
    'os_name': server.get("os", ""),
    'database_name': server.get("db", ""),
    .
    .
    .
}

s = Server(**server_dict)
s.save()

This was for adding a row.
If I wanted to use the same code for updating, is this a good approach ?

if order_id > 0:
	server_dict['id'] = order_id
s = Server(**server_dict)
s.save()

Within the limited context provided by what you’ve posted here, yes, that would work.

1 Like

I am storing this context’s data in a JavaScript global object and then using JavaScript fetch to send the data to a add_api() REST call. I can differentiate between an add and edit by setting order_id to 0 (add) or an existing id (edit).

But I want to set server_dict’s attributes to only those fields that have been edited in the front-end.