I have the following data structure:
{"related_cases": [
{
"type": "related_case_block",
"value": {
"case": 13,
"short_text": "Case 2 is related!"
},
"id": "3aec5efe-55dc-441f-aa5c-fbbb801d237a"
}
]}
Related cases is a streamfield with blocks inside. Each block holds a reference to another case page. Which is 13 in this case.
I would like to include some fields from this case into the response, like this:
{"related_cases": [
{
"type": "related_case_block",
"value": {
"case": {
"id": 13,
"title": "Case 2"
},
"short_text": "Case 2 is related!"
},
"id": "3aec5efe-55dc-441f-aa5c-fbbb801d237a"
}
]}
Can anyone explain how I would accomplish this?
Assuming you've defined related_case_block
as a subclass of StructBlock
, you can override the get_api_representation
method on that class:
class RelatedCaseBlock(blocks.StructBlock):
# ...
def get_api_representation(self, value, context=None):
return {
'case': {
'id': value['case'].id
'title': value['case'].title
},
'short_text': value['short_text']
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加