aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/fields.py
diff options
context:
space:
mode:
authorMarkus Törnqvist2013-05-18 18:13:31 +0300
committerMarkus Törnqvist2013-05-18 18:13:31 +0300
commit48c1b2233bbfb242b68d495f51f0f177b40aa7c4 (patch)
tree86a1630fe959518b53fcd21ccacabb43cb41ff04 /rest_framework/fields.py
parent2795e842d60c4f1759129ab46bad6ba3e62195aa (diff)
parent3f47eb7a77fcc735782dd1bf8e8e053e26417ea1 (diff)
downloaddjango-rest-framework-48c1b2233bbfb242b68d495f51f0f177b40aa7c4.tar.bz2
Merge branch 'master' into mjtorn-master
Conflicts: rest_framework/serializers.py rest_framework/tests/serializer.py Fixed conflicts
Diffstat (limited to 'rest_framework/fields.py')
-rw-r--r--rest_framework/fields.py14
1 files changed, 10 insertions, 4 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
index e4da1456..46fb3152 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -15,10 +15,12 @@ import warnings
from django.core import validators
from django.core.exceptions import ValidationError
from django.conf import settings
+from django.db.models.fields import BLANK_CHOICE_DASH
from django import forms
from django.forms import widgets
from django.utils.encoding import is_protected_type
from django.utils.translation import ugettext_lazy as _
+from django.utils.datastructures import SortedDict
from rest_framework import ISO_8601
from rest_framework.compat import timezone, parse_date, parse_datetime, parse_time
@@ -50,7 +52,7 @@ def get_component(obj, attr_name):
return that attribute on the object.
"""
if isinstance(obj, dict):
- val = obj[attr_name]
+ val = obj.get(attr_name)
else:
val = getattr(obj, attr_name)
@@ -176,7 +178,11 @@ class Field(object):
elif hasattr(value, '__iter__') and not isinstance(value, (dict, six.string_types)):
return [self.to_native(item) for item in value]
elif isinstance(value, dict):
- return dict(map(self.to_native, (k, v)) for k, v in value.items())
+ # Make sure we preserve field ordering, if it exists
+ ret = SortedDict()
+ for key, val in value.items():
+ ret[key] = self.to_native(val)
+ return ret
return smart_text(value)
def attributes(self):
@@ -384,7 +390,6 @@ class URLField(CharField):
type_name = 'URLField'
def __init__(self, **kwargs):
- kwargs['max_length'] = kwargs.get('max_length', 200)
kwargs['validators'] = [validators.URLValidator()]
super(URLField, self).__init__(**kwargs)
@@ -393,7 +398,6 @@ class SlugField(CharField):
type_name = 'SlugField'
def __init__(self, *args, **kwargs):
- kwargs['max_length'] = kwargs.get('max_length', 50)
super(SlugField, self).__init__(*args, **kwargs)
@@ -409,6 +413,8 @@ class ChoiceField(WritableField):
def __init__(self, choices=(), *args, **kwargs):
super(ChoiceField, self).__init__(*args, **kwargs)
self.choices = choices
+ if not self.required:
+ self.choices = BLANK_CHOICE_DASH + self.choices
def _get_choices(self):
return self._choices