Created
June 12, 2015 10:57
-
-
Save willstott101/f668dfd78e44698b8966 to your computer and use it in GitHub Desktop.
Very simple fixed value field for DRF.
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
from rest_framework import serializers | |
class ConstantField(serializers.ReadOnlyField): | |
def __init__(self, value, *args, **kwargs): | |
self._constant = value | |
super(ConstantField, self).__init__(*args, **kwargs) | |
def get_attribute(self, obj): | |
return self._constant | |
def to_representation(self, value): | |
return value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment