Use new value_from_object in preference to internal _get_val_from_obj

This commit is contained in:
Tom Christie 2016-06-02 14:13:50 +01:00
parent 745a28e8b1
commit b6a7c527a8
2 changed files with 8 additions and 2 deletions

View File

@ -122,6 +122,12 @@ def get_related_model(field):
return field.remote_field.model 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. # contrib.postgres only supported from 1.8 onwards.
try: try:
from django.contrib.postgres import fields as postgres_fields from django.contrib.postgres import fields as postgres_fields

View File

@ -31,7 +31,7 @@ from django.utils.translation import ugettext_lazy as _
from rest_framework import ISO_8601 from rest_framework import ISO_8601
from rest_framework.compat import ( 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.exceptions import ValidationError
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
@ -1687,7 +1687,7 @@ class ModelField(Field):
return obj return obj
def to_representation(self, 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): if is_protected_type(value):
return value return value
return self.model_field.value_to_string(obj) return self.model_field.value_to_string(obj)