Skip to content

Instantly share code, notes, and snippets.

@willstott101
Created June 12, 2015 10:57
Show Gist options
  • Save willstott101/f668dfd78e44698b8966 to your computer and use it in GitHub Desktop.
Save willstott101/f668dfd78e44698b8966 to your computer and use it in GitHub Desktop.
Very simple fixed value field for DRF.
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