xeor
xeor

Reputation: 5455

Change model-object at PUT and update before GET in django-rest-framework

Sorry about the confusing title, but I don't know how to describe it better.

I need to run a model-function on the object I am editing using PUT in Django-Rest-Framework, which uses some of the new data from the PUT to calculate some new values it should save in the same model.

Example:

I need the response to be 2 in the response from the PUT, not after a regrab of the object. But how?

I have tried several things (which all doesn’t work):

Does this look like a bug? Or is there another trick?

Upvotes: 1

Views: 1114

Answers (1)

xeor
xeor

Reputation: 5455

I kinda found a solution, but it feels somewhat dirty..

In my serializers restore_object I put code like this:

new_values = instance.update_counters()
for k, v in new_values.items():
    self.data[k] = v

and in my models update_counters() function, I am returning a dict of what I changed..

Upvotes: 1

Related Questions