diff options
| author | Karol Majta | 2013-05-18 16:56:38 +0200 |
|---|---|---|
| committer | Karol Majta | 2013-05-18 16:56:38 +0200 |
| commit | 5bebd29f11dd9268b9a23c27cf58c8440664f5e9 (patch) | |
| tree | 66f75d176f8746432cf9083c0bdfe92a068e142f /rest_framework/fields.py | |
| parent | ebe959b52a10a88975b15c69275b0ef5c50cb9fa (diff) | |
| parent | 3f47eb7a77fcc735782dd1bf8e8e053e26417ea1 (diff) | |
| download | django-rest-framework-5bebd29f11dd9268b9a23c27cf58c8440664f5e9.tar.bz2 | |
Merge branch 'master' of git://github.com/tomchristie/django-rest-framework
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py index c83ee5ec..fc14184c 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) @@ -170,7 +172,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): @@ -377,7 +383,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) @@ -386,7 +391,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) @@ -402,6 +406,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 |
