mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Relational fields use same field_to_native logic as regular fields.
Fixes #637. Closes #638.
This commit is contained in:
parent
f341ead499
commit
018298deb8
|
@ -5,7 +5,7 @@ from django import forms
|
||||||
from django.forms import widgets
|
from django.forms import widgets
|
||||||
from django.forms.models import ModelChoiceIterator
|
from django.forms.models import ModelChoiceIterator
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from rest_framework.fields import Field, WritableField
|
from rest_framework.fields import Field, WritableField, get_component
|
||||||
from rest_framework.reverse import reverse
|
from rest_framework.reverse import reverse
|
||||||
from rest_framework.compat import urlparse
|
from rest_framework.compat import urlparse
|
||||||
from rest_framework.compat import smart_text
|
from rest_framework.compat import smart_text
|
||||||
|
@ -116,7 +116,16 @@ class RelatedField(WritableField):
|
||||||
|
|
||||||
def field_to_native(self, obj, field_name):
|
def field_to_native(self, obj, field_name):
|
||||||
try:
|
try:
|
||||||
value = getattr(obj, self.source or field_name)
|
if self.source == '*':
|
||||||
|
return self.to_native(obj)
|
||||||
|
|
||||||
|
source = self.source or field_name
|
||||||
|
value = obj
|
||||||
|
|
||||||
|
for component in source.split('.'):
|
||||||
|
value = get_component(value, component)
|
||||||
|
if value is None:
|
||||||
|
break
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user