From b6a7c527a8528e5bd42036825d0303ae73bf4f92 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 2 Jun 2016 14:13:50 +0100 Subject: [PATCH] Use new value_from_object in preference to internal _get_val_from_obj --- rest_framework/compat.py | 6 ++++++ rest_framework/fields.py | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index c56604862..dd30636f4 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -122,6 +122,12 @@ def get_related_model(field): return field.remote_field.model +def value_from_object(field, obj): + if django.VERSION < (1, 9): + return field._get_val_from_obj(obj) + field.value_from_object(obj) + + # contrib.postgres only supported from 1.8 onwards. try: from django.contrib.postgres import fields as postgres_fields diff --git a/rest_framework/fields.py b/rest_framework/fields.py index f00fe10c0..68e4cdf91 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -31,7 +31,7 @@ from django.utils.translation import ugettext_lazy as _ from rest_framework import ISO_8601 from rest_framework.compat import ( - get_remote_field, unicode_repr, unicode_to_repr + get_remote_field, unicode_repr, unicode_to_repr, value_from_object ) from rest_framework.exceptions import ValidationError from rest_framework.settings import api_settings @@ -1687,7 +1687,7 @@ class ModelField(Field): return obj def to_representation(self, obj): - value = self.model_field._get_val_from_obj(obj) + value = value_from_object(self.model_field, obj) if is_protected_type(value): return value return self.model_field.value_to_string(obj)