aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/serializers.py
diff options
context:
space:
mode:
authorTom Christie2014-01-13 17:38:03 +0000
committerTom Christie2014-01-13 17:38:03 +0000
commite91d0a69ab404a79da1dd5d0806d33e972a1a09b (patch)
tree16b002eeed52d39b7d803cb62c606edbddd821e9 /rest_framework/serializers.py
parentd937ce331cc6cd2df04a989b49cca030f65319da (diff)
parentc4d77667cf80588a2195fdc025bda53a5b977105 (diff)
downloaddjango-rest-framework-e91d0a69ab404a79da1dd5d0806d33e972a1a09b.tar.bz2
Merge branch 'testing-nested-serializers' of git://github.com/dustinfarris/django-rest-framework into dustinfarris-testing-nested-serializers
Diffstat (limited to 'rest_framework/serializers.py')
-rw-r--r--rest_framework/serializers.py28
1 files changed, 27 insertions, 1 deletions
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py
index b22ca578..0ea2cadb 100644
--- a/rest_framework/serializers.py
+++ b/rest_framework/serializers.py
@@ -13,8 +13,10 @@ response content is handled by parsers and renderers.
from __future__ import unicode_literals
import copy
import datetime
+import inspect
import types
from decimal import Decimal
+from django.core.exceptions import ImproperlyConfigured
from django.core.paginator import Page
from django.db import models
from django.forms import widgets
@@ -32,6 +34,27 @@ from rest_framework.relations import *
from rest_framework.fields import *
+def _resolve_model(obj):
+ """
+ Resolve supplied `obj` to a Django model class.
+
+ `obj` must be a Django model class itself, or a string
+ representation of one. Useful in situtations like GH #1225 where
+ Django may not have resolved a string-based reference to a model in
+ another model's foreign key definition.
+
+ String representations should have the format:
+ 'appname.ModelName'
+ """
+ if type(obj) == str and len(obj.split('.')) == 2:
+ app_name, model_name = obj.split('.')
+ return models.get_model(app_name, model_name)
+ elif inspect.isclass(obj) and issubclass(obj, models.Model):
+ return obj
+ else:
+ raise ValueError("{0} is not a Django model".format(obj))
+
+
def pretty_name(name):
"""Converts 'first_name' to 'First name'"""
if not name:
@@ -656,7 +679,10 @@ class ModelSerializer(Serializer):
if model_field.rel:
to_many = isinstance(model_field,
models.fields.related.ManyToManyField)
- related_model = model_field.rel.to
+ try:
+ related_model = _resolve_model(model_field.rel.to)
+ except ValueError as error_message:
+ raise ImproperlyConfigured(error_message)
if to_many and not model_field.rel.through._meta.auto_created:
has_through_model = True