aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/relations.py
diff options
context:
space:
mode:
authorTom Christie2014-10-09 15:11:19 +0100
committerTom Christie2014-10-09 15:11:19 +0100
commit5d247a65c89594a7ab5ce2333612f23eadc6828d (patch)
treed9e67e3a84a588747cd6e39356151149cf73b376 /rest_framework/relations.py
parentbabdc78e61ac915fa4a01bdfb04e11a32dbf5d79 (diff)
downloaddjango-rest-framework-5d247a65c89594a7ab5ce2333612f23eadc6828d.tar.bz2
First pass on nested serializers in HTML
Diffstat (limited to 'rest_framework/relations.py')
-rw-r--r--rest_framework/relations.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/rest_framework/relations.py b/rest_framework/relations.py
index c1e5aa18..268b95cf 100644
--- a/rest_framework/relations.py
+++ b/rest_framework/relations.py
@@ -1,6 +1,7 @@
from rest_framework.compat import smart_text, urlparse
from rest_framework.fields import empty, Field
from rest_framework.reverse import reverse
+from rest_framework.utils import html
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
from django.core.urlresolvers import resolve, get_script_prefix, NoReverseMatch, Resolver404
from django.db.models.query import QuerySet
@@ -263,6 +264,13 @@ class ManyRelation(Field):
super(ManyRelation, self).__init__(*args, **kwargs)
self.child_relation.bind(field_name='', parent=self)
+ def get_value(self, dictionary):
+ # We override the default field access in order to support
+ # lists in HTML forms.
+ if html.is_html_input(dictionary):
+ return dictionary.getlist(self.field_name)
+ return dictionary.get(self.field_name, empty)
+
def to_internal_value(self, data):
return [
self.child_relation.to_internal_value(item)
@@ -278,10 +286,16 @@ class ManyRelation(Field):
@property
def choices(self):
+ queryset = self.child_relation.queryset
+ iterable = queryset.all() if (hasattr(queryset, 'all')) else queryset
+ items_and_representations = [
+ (item, self.child_relation.to_representation(item))
+ for item in iterable
+ ]
return dict([
(
- str(self.child_relation.to_representation(item)),
- str(item)
+ str(item_representation),
+ str(item) + ' - ' + str(item_representation)
)
- for item in self.child_relation.queryset.all()
+ for item, item_representation in items_and_representations
])