-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add extra fields options to relationships
This change is applied to course-experience api. The relations file inherit from json api relation but add the possibility to add some extra fields using a function. The function has to be passed through the new relation using `get_extra_fields` kwarg. There you can calculate a dict that would be added to corresponding relationhips field. The base json api only add id and type, with this change you cad add extra model attributes. https://github.com/django-json-api/django-rest-framework-json-api/blob/main/rest_framework_json_api/relations.py#L255 chore: pr suggestions Co-authored-by: Andrey Cañon <[email protected]>
- Loading branch information
1 parent
034ba4f
commit a345957
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Relations used for customize experience views. The relations are used to specify how to manage | ||
the relations fields in the serializers. | ||
https://github.com/encode/django-rest-framework/blob/master/rest_framework/relations.py | ||
""" | ||
from rest_framework_json_api.relations import ResourceRelatedField | ||
|
||
|
||
class ExperienceResourceRelatedField(ResourceRelatedField): | ||
"""Class to configure relations for course experience API. | ||
Ancestors: | ||
relation (ResourceRelatedField): the ResourceRelatedField relation from json api | ||
""" | ||
def __init__(self, **kwargs): | ||
""" Include an additional kwargs parameter to manage the extra model fields to be shown. | ||
The value of the kwarg should be a function with kwargs accepting value: (value=instance). | ||
get_extra_fields (function) | ||
""" | ||
self.get_extra_fields = kwargs.pop('get_extra_fields', None) | ||
super().__init__(**kwargs) | ||
|
||
def to_representation(self, value): | ||
"""Add to the base json api representation extra fields apart from `id` and `type` | ||
using a function passed via `self.get_extra_fields` | ||
https://github.com/django-json-api/django-rest-framework-json-api/blob/main/rest_framework_json_api/relations.py#L255 | ||
The attributes shape is based on | ||
https://jsonapi.org/format/#document-resource-objects | ||
Args: | ||
value (instance model): instance of foreign model extracted from relation | ||
Returns: | ||
json_api_representation (ordered-dict): json api representation with extra model data in attributes field. | ||
""" | ||
json_api_representation = super().to_representation(value) | ||
|
||
if self.get_extra_fields and callable(self.get_extra_fields): | ||
json_api_representation.update(self.get_extra_fields(value=value)) | ||
|
||
return json_api_representation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters