I'm using Django Rest Framework and need to add extra data to a result set. Specifically, where you would usually have:
{
"count": 45,
"next": "http://localhost:8000/foo/bar?page=2",
"previous": null,
"results": [
{...}
]
}
I would like to add extra counts like so:
{
"count": 45,
"10_mi_count": 10,
"20_mi_count": 30,
"30_mi_count": 45,
"next": "http://localhost:8000/foo/bar?page=2",
"previous": null,
"results": [
{...}
]
}
The extra counts in this example are just how many of the objects have a field distance with a value less than the miles described in the key.
My issue is that I have no idea where the best place to extend and insert this behaviour is.
Ideally I'd like this to work whether or not the results are paginated, making no assumptions.
What I'm really after here is a nod in the right direction (and why that is the right place for it go).
I've checked the docs and can't find anything that describes how to add things like this, but I would be more than happy to be proven wrong on that score.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…