Change model-object at PUT and update before GET in django-rest-framework -
sorry confusing title, don't know how describe better.
i need run model-function on object editing using put in django-rest-framework, uses of new data put calculate new values should save in same model.
example:
- an item
{'amount': 2, 'price': 0, 'total': 0}stored in database. - i updating price 1 using normal put request using django-rest-framework.
- the model have helperfunction called
update_total()need call update total field in database (to, in case 2 (2*1)). - the item updated in database, response returned django-rest-framework still showing
total=0. after getting object on new, total 2 expected.
i need response 2 in response put, not after regrab of object. how?
i have tried several things (which doesn’t work):
- updating attrs in validator new value.
- using
post_save()inlistcreateapiviewupdate data. - using
pre_save()inlistcreateapiview - updating instance in
restore_object()(even though isn't purpose)
does bug? or there trick?
i kinda found solution, feels dirty..
in serializers restore_object put code this:
new_values = instance.update_counters() k, v in new_values.items(): self.data[k] = v and in models update_counters() function, returning dict of changed..
Comments
Post a Comment