A
A
Andrew2020-05-21 13:34:32
Django
Andrew, 2020-05-21 13:34:32

How to add field to serializer outside of model?

Ladies and gentlemen, perhaps I do not quite understand the very essence of serialization.

The task is as follows: in a view (ordinary) when I issue a list of transactions, I count separately how much was received from the counterparty, how much went to the counterparty, I put it in the context and display it in the template.
I do not understand how to do the same in the REST framework?
I figured out how to add a custom field, but it is added to each element of the list. And I need to add to the final json fields that the server will count, but which do not apply to each element of the list, but are their total.
Or perhaps it is considered practice to do such calculations on the client side? But I don't see the point in that. Moreover, if I can get the data by direct queries in mysql and I just need to add them to serializer.data

I hope I made it clear.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2020-05-21
@G_r_i_n_v_i_c_h

The idea, as far as I understand, in the output at the bottom of the plate is something like the line total? for DRF, here you need to write a paginator class, which, in addition to the list of results, will also give out the sum of all (in general, all, and not just from the current page, if only for the current one, then you can calculate it on the client).
Or option 2 - just form the desired json with the handles and give in the answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question