Created
November 18, 2018 21:01
-
-
Save kdeloach/47cd6c2cb71ee18a4879825631312360 to your computer and use it in GitHub Desktop.
CustomJSONRenderer
This file contains hidden or 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
class CustomJSONRenderer(rest_framework_json_api.renderers.JSONRenderer): | |
""" | |
Replace references to primary key with `uuid` field value. | |
""" | |
@classmethod | |
def extract_attributes(cls, fields, resource): | |
obj = super(CustomJSONRenderer, cls).extract_attributes(fields, resource) | |
obj.pop('uuid') | |
return obj | |
@classmethod | |
def build_json_resource_obj(cls, fields, resource, resource_instance, *args, **kwargs): | |
obj = super(CustomJSONRenderer, cls).build_json_resource_obj(fields, resource, resource_instance, *args, **kwargs) | |
obj['id'] = str(resource_instance.uuid) | |
return obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment