django update model while saving another form -
i saving , updating model in form, update not saved database.
if request.method == 'post': form = inventorytransactionform(request.post, instance=inventorytransaction()) if form.is_valid(): quantity = request.post['quantity'] part_id = request.post['part_id'] item_template_id = request.post['supply'] try: item_object = item.objects.get(pk=part_id) masterqty = item_object.masterqty - int(quantity) item_object.save(force_update=true) except exception, e: messages.error(request, e.message)
can please help wrong in above code, thanks.
edit: updated code below
@transaction.commit_manually def post(self, request, *args, **kwargs): if request.method == 'post': form = inventorytransactionform(request.post, instance=inventorytransaction()) if form.is_valid(): quantity = request.post['quantity'] part_id = request.post['part_id'] item_template_id = request.post['supply'] try: item_object = item.objects.get(pk=part_id) masterqty = item_object.masterqty - int(quantity) item_object.save(force_update=true) transaction.commit() except exception, e: transaction.rollback() messages.error(request, e.message) form.save(true)
you using force_update=true
in save method. docs:
in rare circumstances, it’s necessary able forcefulness save() method perform sql insert , not fall doing update. or vice-versa: update, if possible, not insert new row. in these cases can pass force_insert=true or force_update=true parameters save() method. obviously, passing both parameters error: cannot both insert , update @ same time!
you saving object database first time doesn't create sense phone call force_update=true
prevent object beingness created in first place. alter
item_object.save(force_update=true)
to
item_object.save()
django django-models
No comments:
Post a Comment