2👍
This is not how you’re supposed to update.
Model(**param_dict["Model"]).save()
You’re creating a new instance with the same id
. Instead, you should get
the instance, and then update it appropriately.
m = Model.objects.get(id=param_dict['id'])
m.field = param_dict['some_field']
m.save()
Or, you can use the Manager update method:
Model.objects.filter(id=param_dict['id']).update(**param_dict['Model'])
There’s also the get_or_create method if you’re not sure whether or not the record already exists.
You can try using a REST framework, like tasty-pie or django-rest-framework, which might alleviate some problems you’re having.
Edit:
A brief summary about how save
works in django. This is what I meant about whether or not an INSERT
or an UPDATE
is happening. Unless your post_data
dict contains empty values for all the fields, read the documentation on how save
works for a more thorough understanding of how django works.
So, what is happening in your case is this:
dict = {'id': 1, 'field1': 'my_value'}
m = Model(**dict)
m.id # 1
m.field1 # my_value
m.field2 # None (because you haven't set it, it defaults to None
m.save() # UPDATEs the existing instance with id 1 with ALL of the values of `m`
So, you’re saving an instance that contains None
values. That’s why I’m suggesting you do a get
, so that all the correct values are filled, before saving to the database.
0👍
Maybe you shoul use some function like this:
def insert_or_update(param_dict):
pk = param_dict.get('pk', None)
if pk:
Model.objects.filter(pk=pk).update(**param_dict)
else:
Model(**param_dict)
Model.save()
- [Answered ]-Python django redirect url on render
- [Answered ]-Confirmation form step2.html Django
- [Answered ]-Python Cursor SELECT columns dynamically
- [Answered ]-Django-countries and TastyPie: Get country name
- [Answered ]-Internal Server Error on Django after I changed database