aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Christie2015-01-30 14:00:25 +0000
committerTom Christie2015-01-30 14:00:25 +0000
commit4ee4b4f2dc3cffd8246772bdf829a2a720493377 (patch)
tree95b514483e901c10bdeee0d66e60430002d5101a
parent8f33e39f62c59d06783dd3e6e7cf8d464ff08c5f (diff)
parent7cf9dea7f905ea6869148a68b4fa96cad0a347e8 (diff)
downloaddjango-rest-framework-4ee4b4f2dc3cffd8246772bdf829a2a720493377.tar.bz2
Merge master
-rw-r--r--docs/api-guide/fields.md29
-rw-r--r--docs/api-guide/filtering.md4
-rwxr-xr-xdocs/api-guide/generic-views.md8
-rw-r--r--docs/api-guide/parsers.md2
-rw-r--r--docs/api-guide/routers.md12
-rw-r--r--docs/api-guide/viewsets.md2
-rw-r--r--docs/img/sponsors/2-galileo_press.pngbin11451 -> 0 bytes
-rw-r--r--docs/img/sponsors/2-rheinwerk_verlag.pngbin0 -> 1562 bytes
-rw-r--r--docs/index.md2
-rw-r--r--docs/topics/3.0-announcement.md2
-rw-r--r--docs/topics/kickstarter-announcement.md2
-rw-r--r--docs/topics/release-notes.md38
-rw-r--r--requirements.txt2
-rw-r--r--rest_framework/__init__.py2
-rw-r--r--rest_framework/authtoken/south_migrations/0001_initial.py2
-rw-r--r--rest_framework/compat.py7
-rw-r--r--rest_framework/fields.py77
-rw-r--r--rest_framework/parsers.py2
-rw-r--r--rest_framework/relations.py5
-rw-r--r--rest_framework/routers.py10
-rw-r--r--rest_framework/serializers.py12
-rw-r--r--rest_framework/settings.py11
-rw-r--r--rest_framework/utils/field_mapping.py3
-rw-r--r--tests/test_fields.py69
-rw-r--r--tests/test_filters.py16
-rw-r--r--tests/test_relations.py33
-rw-r--r--tests/test_routers.py7
-rw-r--r--tests/utils.py25
-rw-r--r--tox.ini2
29 files changed, 316 insertions, 70 deletions
diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md
index b3d274dd..10291c12 100644
--- a/docs/api-guide/fields.md
+++ b/docs/api-guide/fields.md
@@ -182,6 +182,12 @@ Corresponds to `django.db.models.fields.URLField`. Uses Django's `django.core.v
**Signature:** `URLField(max_length=200, min_length=None, allow_blank=False)`
+## UUIDField
+
+A field that ensures the input is a valid UUID string. The `to_internal_value` method will return a `uuid.UUID` instance. On output the field will return a string in the canonical hyphenated format, for example:
+
+ "de305d54-75b4-431b-adb2-eb6b9e546013"
+
---
# Numeric fields
@@ -320,7 +326,7 @@ Both the `allow_blank` and `allow_null` are valid options on `ChoiceField`, alth
## MultipleChoiceField
-A field that can accept a set of zero, one or many values, chosen from a limited set of choices. Takes a single mandatory argument. `to_internal_representation` returns a `set` containing the selected values.
+A field that can accept a set of zero, one or many values, chosen from a limited set of choices. Takes a single mandatory argument. `to_internal_value` returns a `set` containing the selected values.
**Signature:** `MultipleChoiceField(choices)`
@@ -374,7 +380,7 @@ A field class that validates a list of objects.
**Signature**: `ListField(child)`
-- `child` - A field instance that should be used for validating the objects in the list.
+- `child` - A field instance that should be used for validating the objects in the list. If this argument is not provided then objects in the list will not be validated.
For example, to validate a list of integers you might use something like the following:
@@ -389,6 +395,23 @@ The `ListField` class also supports a declarative style that allows you to write
We can now reuse our custom `StringListField` class throughout our application, without having to provide a `child` argument to it.
+## DictField
+
+A field class that validates a dictionary of objects. The keys in `DictField` are always assumed to be string values.
+
+**Signature**: `DictField(child)`
+
+- `child` - A field instance that should be used for validating the values in the dictionary. If this argument is not provided then values in the mapping will not be validated.
+
+For example, to create a field that validates a mapping of strings to strings, you would write something like this:
+
+ document = DictField(child=CharField())
+
+You can also use the declarative style, as with `ListField`. For example:
+
+ class DocumentField(DictField):
+ child = CharField()
+
---
# Miscellaneous fields
@@ -438,7 +461,7 @@ This is a read-only field. It gets its value by calling a method on the serializ
**Signature**: `SerializerMethodField(method_name=None)`
-- `method-name` - The name of the method on the serializer to be called. If not included this defaults to `get_<field_name>`.
+- `method_name` - The name of the method on the serializer to be called. If not included this defaults to `get_<field_name>`.
The serializer method referred to by the `method_name` argument should accept a single argument (in addition to `self`), which is the object being serialized. It should return whatever you want to be included in the serialized representation of the object. For example:
diff --git a/docs/api-guide/filtering.md b/docs/api-guide/filtering.md
index e00560c7..b16b6be5 100644
--- a/docs/api-guide/filtering.md
+++ b/docs/api-guide/filtering.md
@@ -398,8 +398,8 @@ The [django-rest-framework-filters package][django-rest-framework-filters] works
[cite]: https://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-specific-objects-with-filters
[django-filter]: https://github.com/alex/django-filter
[django-filter-docs]: https://django-filter.readthedocs.org/en/latest/index.html
-[guardian]: http://pythonhosted.org/django-guardian/
-[view-permissions]: http://pythonhosted.org/django-guardian/userguide/assign.html
+[guardian]: https://django-guardian.readthedocs.org/
+[view-permissions]: https://django-guardian.readthedocs.org/en/latest/userguide/assign.html
[view-permissions-blogpost]: http://blog.nyaruka.com/adding-a-view-permission-to-django-models
[nullbooleanselect]: https://github.com/django/django/blob/master/django/forms/widgets.py
[search-django-admin]: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields
diff --git a/docs/api-guide/generic-views.md b/docs/api-guide/generic-views.md
index 6374e305..61c8e8d8 100755
--- a/docs/api-guide/generic-views.md
+++ b/docs/api-guide/generic-views.md
@@ -93,17 +93,13 @@ The following attributes are used to control pagination when used with list view
* `filter_backends` - A list of filter backend classes that should be used for filtering the queryset. Defaults to the same value as the `DEFAULT_FILTER_BACKENDS` setting.
-**Deprecated attributes**:
-
-* `model` - This shortcut may be used instead of setting either (or both) of the `queryset`/`serializer_class` attributes. The explicit style is preferred over the `.model` shortcut, and usage of this attribute is now deprecated.
-
### Methods
**Base methods**:
#### `get_queryset(self)`
-Returns the queryset that should be used for list views, and that should be used as the base for lookups in detail views. Defaults to returning the queryset specified by the `queryset` attribute, or the default queryset for the model if the `model` shortcut is being used.
+Returns the queryset that should be used for list views, and that should be used as the base for lookups in detail views. Defaults to returning the queryset specified by the `queryset` attribute.
This method should always be used rather than accessing `self.queryset` directly, as `self.queryset` gets evaluated only once, and those results are cached for all subsequent requests.
@@ -153,7 +149,7 @@ For example:
#### `get_serializer_class(self)`
-Returns the class that should be used for the serializer. Defaults to returning the `serializer_class` attribute, or dynamically generating a serializer class if the `model` shortcut is being used.
+Returns the class that should be used for the serializer. Defaults to returning the `serializer_class` attribute.
May be overridden to provide dynamic behavior, such as using different serializers for read and write operations, or providing different serializers to different types of users.
diff --git a/docs/api-guide/parsers.md b/docs/api-guide/parsers.md
index b68b33be..c242f878 100644
--- a/docs/api-guide/parsers.md
+++ b/docs/api-guide/parsers.md
@@ -108,7 +108,7 @@ If the view used with `FileUploadParser` is called with a `filename` URL keyword
def put(self, request, filename, format=None):
file_obj = request.data['file']
# ...
- # do some staff with uploaded file
+ # do some stuff with uploaded file
# ...
return Response(status=204)
diff --git a/docs/api-guide/routers.md b/docs/api-guide/routers.md
index 9c9bfb50..592f7d66 100644
--- a/docs/api-guide/routers.md
+++ b/docs/api-guide/routers.md
@@ -28,7 +28,7 @@ There are two mandatory arguments to the `register()` method:
Optionally, you may also specify an additional argument:
-* `base_name` - The base to use for the URL names that are created. If unset the basename will be automatically generated based on the `model` or `queryset` attribute on the viewset, if it has one. Note that if the viewset does not include a `model` or `queryset` attribute then you must set `base_name` when registering the viewset.
+* `base_name` - The base to use for the URL names that are created. If unset the basename will be automatically generated based on the `queryset` attribute of the viewset, if it has one. Note that if the viewset does not include a `queryset` attribute then you must set `base_name` when registering the viewset.
The example above would generate the following URL patterns:
@@ -60,7 +60,7 @@ For example, you can append `router.urls` to a list of existing views…
router.register(r'accounts', AccountViewSet)
urlpatterns = [
- url(r'^forgot-password/$', ForgotPasswordFormView.as_view(),
+ url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
]
urlpatterns += router.urls
@@ -68,15 +68,15 @@ For example, you can append `router.urls` to a list of existing views…
Alternatively you can use Django's `include` function, like so…
urlpatterns = [
- url(r'^forgot-password/$', ForgotPasswordFormView.as_view(),
- url(r'^', include(router.urls))
+ url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
+ url(r'^', include(router.urls)),
]
Router URL patterns can also be namespaces.
urlpatterns = [
- url(r'^forgot-password/$', ForgotPasswordFormView.as_view(),
- url(r'^api/', include(router.urls, namespace='api'))
+ url(r'^forgot-password/$', ForgotPasswordFormView.as_view()),
+ url(r'^api/', include(router.urls, namespace='api')),
]
If using namespacing with hyperlinked serializers you'll also need to ensure that any `view_name` parameters on the serializers correctly reflect the namespace. In the example above you'd need to include a parameter such as `view_name='api:user-detail'` for serializer fields hyperlinked to the user detail view.
diff --git a/docs/api-guide/viewsets.md b/docs/api-guide/viewsets.md
index 3e37cef8..b09dfc9e 100644
--- a/docs/api-guide/viewsets.md
+++ b/docs/api-guide/viewsets.md
@@ -146,7 +146,7 @@ The decorators can additionally take extra arguments that will be set for the ro
def set_password(self, request, pk=None):
...
-Theses decorators will route `GET` requests by default, but may also accept other HTTP methods, by using the `methods` argument. For example:
+These decorators will route `GET` requests by default, but may also accept other HTTP methods, by using the `methods` argument. For example:
@detail_route(methods=['post', 'delete'])
def unset_password(self, request, pk=None):
diff --git a/docs/img/sponsors/2-galileo_press.png b/docs/img/sponsors/2-galileo_press.png
deleted file mode 100644
index f77e6c0a..00000000
--- a/docs/img/sponsors/2-galileo_press.png
+++ /dev/null
Binary files differ
diff --git a/docs/img/sponsors/2-rheinwerk_verlag.png b/docs/img/sponsors/2-rheinwerk_verlag.png
new file mode 100644
index 00000000..ad454e17
--- /dev/null
+++ b/docs/img/sponsors/2-rheinwerk_verlag.png
Binary files differ
diff --git a/docs/index.md b/docs/index.md
index efd99a4e..c3a9c2d4 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -55,7 +55,7 @@ REST framework requires the following:
The following packages are optional:
* [Markdown][markdown] (2.1.0+) - Markdown support for the browsable API.
-* [django-filter][django-filter] (0.5.4+) - Filtering support.
+* [django-filter][django-filter] (0.9.2+) - Filtering support.
* [django-guardian][django-guardian] (1.1.1+) - Object level permissions support.
## Installation
diff --git a/docs/topics/3.0-announcement.md b/docs/topics/3.0-announcement.md
index 5dbc5600..24e69c2d 100644
--- a/docs/topics/3.0-announcement.md
+++ b/docs/topics/3.0-announcement.md
@@ -665,7 +665,7 @@ This code *would be valid* in `2.4.3`:
class Meta:
model = Account
-However this code *would not be valid* in `2.4.3`:
+However this code *would not be valid* in `3.0`:
# Missing `queryset`
class AccountSerializer(serializers.Serializer):
diff --git a/docs/topics/kickstarter-announcement.md b/docs/topics/kickstarter-announcement.md
index 9f99e3e6..78c5cce6 100644
--- a/docs/topics/kickstarter-announcement.md
+++ b/docs/topics/kickstarter-announcement.md
@@ -84,7 +84,7 @@ Our gold sponsors include companies large and small. Many thanks for their signi
<li><a href="http://pulsecode.ca" rel="nofollow" style="background-image:url(../../img/sponsors/2-pulsecode.png);">Pulsecode Inc.</a></li>
<li><a href="http://singinghorsestudio.com" rel="nofollow" style="background-image:url(../../img/sponsors/2-singing-horse.png);">Singing Horse Studio Ltd.</a></li>
<li><a href="https://www.heroku.com/" rel="nofollow" style="background-image:url(../../img/sponsors/2-heroku.png);">Heroku</a></li>
-<li><a href="https://www.galileo-press.de/" rel="nofollow" style="background-image:url(../../img/sponsors/2-galileo_press.png);">Galileo Press</a></li>
+<li><a href="https://www.rheinwerk-verlag.de/" rel="nofollow" style="background-image:url(../../img/sponsors/2-rheinwerk_verlag.png);">Rheinwerk Verlag</a></li>
<li><a href="http://www.securitycompass.com/" rel="nofollow" style="background-image:url(../../img/sponsors/2-security_compass.png);">Security Compass</a></li>
<li><a href="https://www.djangoproject.com/foundation/" rel="nofollow" style="background-image:url(../../img/sponsors/2-django.png);">Django Software Foundation</a></li>
<li><a href="http://www.hipflaskapp.com" rel="nofollow" style="background-image:url(../../img/sponsors/2-hipflask.png);">Hipflask</a></li>
diff --git a/docs/topics/release-notes.md b/docs/topics/release-notes.md
index c49dd62c..e0894d2d 100644
--- a/docs/topics/release-notes.md
+++ b/docs/topics/release-notes.md
@@ -41,6 +41,24 @@ You can determine your currently installed version using `pip freeze`:
## 3.0.x series
+### 3.0.4
+
+**Date**: [28th January 2015][3.0.4-milestone].
+
+* Django 1.8a1 support. ([#2425][gh2425], [#2446][gh2446], [#2441][gh2441])
+* Add `DictField` and support Django 1.8 `HStoreField`. ([#2451][gh2451], [#2106][gh2106])
+* Add `UUIDField` and support Django 1.8 `UUIDField`. ([#2448][gh2448], [#2433][gh2433], [#2432][gh2432])
+* `BaseRenderer.render` now raises `NotImplementedError`. ([#2434][gh2434])
+* Fix timedelta JSON serialization on Python 2.6. ([#2430][gh2430])
+* `ResultDict` and `ResultList` now appear as standard dict/list. ([#2421][gh2421])
+* Fix visible `HiddenField` in the HTML form of the web browsable API page. ([#2410][gh2410])
+* Use `OrderedDict` for `RelatedField.choices`. ([#2408][gh2408])
+* Fix ident format when using `HTTP_X_FORWARDED_FOR`. ([#2401][gh2401])
+* Fix invalid key with memcached while using throttling. ([#2400][gh2400])
+* Fix `FileUploadParser` with version 3.x. ([#2399][gh2399])
+* Fix the serializer inheritance. ([#2388][gh2388])
+* Fix caching issues with `ReturnDict`. ([#2360][gh2360])
+
### 3.0.3
**Date**: [8th January 2015][3.0.3-milestone].
@@ -702,6 +720,7 @@ For older release notes, [please see the GitHub repo](old-release-notes).
[3.0.1-milestone]: https://github.com/tomchristie/django-rest-framework/issues?q=milestone%3A%223.0.1+Release%22
[3.0.2-milestone]: https://github.com/tomchristie/django-rest-framework/issues?q=milestone%3A%223.0.2+Release%22
[3.0.3-milestone]: https://github.com/tomchristie/django-rest-framework/issues?q=milestone%3A%223.0.3+Release%22
+[3.0.4-milestone]: https://github.com/tomchristie/django-rest-framework/issues?q=milestone%3A%223.0.4+Release%22
<!-- 3.0.1 -->
[gh2013]: https://github.com/tomchristie/django-rest-framework/issues/2013
@@ -770,3 +789,22 @@ For older release notes, [please see the GitHub repo](old-release-notes).
[gh2355]: https://github.com/tomchristie/django-rest-framework/issues/2355
[gh2369]: https://github.com/tomchristie/django-rest-framework/issues/2369
[gh2386]: https://github.com/tomchristie/django-rest-framework/issues/2386
+<!-- 3.0.4 -->
+[gh2425]: https://github.com/tomchristie/django-rest-framework/issues/2425
+[gh2446]: https://github.com/tomchristie/django-rest-framework/issues/2446
+[gh2441]: https://github.com/tomchristie/django-rest-framework/issues/2441
+[gh2451]: https://github.com/tomchristie/django-rest-framework/issues/2451
+[gh2106]: https://github.com/tomchristie/django-rest-framework/issues/2106
+[gh2448]: https://github.com/tomchristie/django-rest-framework/issues/2448
+[gh2433]: https://github.com/tomchristie/django-rest-framework/issues/2433
+[gh2432]: https://github.com/tomchristie/django-rest-framework/issues/2432
+[gh2434]: https://github.com/tomchristie/django-rest-framework/issues/2434
+[gh2430]: https://github.com/tomchristie/django-rest-framework/issues/2430
+[gh2421]: https://github.com/tomchristie/django-rest-framework/issues/2421
+[gh2410]: https://github.com/tomchristie/django-rest-framework/issues/2410
+[gh2408]: https://github.com/tomchristie/django-rest-framework/issues/2408
+[gh2401]: https://github.com/tomchristie/django-rest-framework/issues/2401
+[gh2400]: https://github.com/tomchristie/django-rest-framework/issues/2400
+[gh2399]: https://github.com/tomchristie/django-rest-framework/issues/2399
+[gh2388]: https://github.com/tomchristie/django-rest-framework/issues/2388
+[gh2360]: https://github.com/tomchristie/django-rest-framework/issues/2360
diff --git a/requirements.txt b/requirements.txt
index 080206bc..cb044958 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -10,7 +10,7 @@ flake8==2.2.2
# Optional packages
markdown>=2.1.0
django-guardian==1.2.4
-django-filter>=0.5.4
+django-filter>=0.9.2
# wheel for PyPI installs
wheel==0.24.0
diff --git a/rest_framework/__init__.py b/rest_framework/__init__.py
index fdcebb7b..57e5421b 100644
--- a/rest_framework/__init__.py
+++ b/rest_framework/__init__.py
@@ -8,7 +8,7 @@ ______ _____ _____ _____ __
"""
__title__ = 'Django REST framework'
-__version__ = '3.0.3'
+__version__ = '3.0.4'
__author__ = 'Tom Christie'
__license__ = 'BSD 2-Clause'
__copyright__ = 'Copyright 2011-2015 Tom Christie'
diff --git a/rest_framework/authtoken/south_migrations/0001_initial.py b/rest_framework/authtoken/south_migrations/0001_initial.py
index 926de02b..5b927f3e 100644
--- a/rest_framework/authtoken/south_migrations/0001_initial.py
+++ b/rest_framework/authtoken/south_migrations/0001_initial.py
@@ -40,7 +40,7 @@ class Migration(SchemaMigration):
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
"%s.%s" % (User._meta.app_label, User._meta.module_name): {
- 'Meta': {'object_name': User._meta.module_name},
+ 'Meta': {'object_name': User._meta.module_name, 'db_table': repr(User._meta.db_table)},
},
'authtoken.token': {
'Meta': {'object_name': 'Token'},
diff --git a/rest_framework/compat.py b/rest_framework/compat.py
index a6620057..50f37014 100644
--- a/rest_framework/compat.py
+++ b/rest_framework/compat.py
@@ -63,6 +63,13 @@ except ImportError:
from django.http import HttpResponse as HttpResponseBase
+# contrib.postgres only supported from 1.8 onwards.
+try:
+ from django.contrib.postgres import fields as postgres_fields
+except ImportError:
+ postgres_fields = None
+
+
# request only provides `resolver_match` from 1.5 onwards.
def get_resolver_match(request):
try:
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
index 342564d3..02d2adef 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -23,6 +23,7 @@ import datetime
import decimal
import inspect
import re
+import uuid
class empty:
@@ -632,6 +633,23 @@ class URLField(CharField):
self.validators.append(validator)
+class UUIDField(Field):
+ default_error_messages = {
+ 'invalid': _('"{value}" is not a valid UUID.'),
+ }
+
+ def to_internal_value(self, data):
+ if not isinstance(data, uuid.UUID):
+ try:
+ return uuid.UUID(data)
+ except (ValueError, TypeError):
+ self.fail('invalid', value=data)
+ return data
+
+ def to_representation(self, value):
+ return str(value)
+
+
# Number types...
class IntegerField(Field):
@@ -1113,8 +1131,21 @@ class ImageField(FileField):
# Composite field types...
+class _UnvalidatedField(Field):
+ def __init__(self, *args, **kwargs):
+ super(_UnvalidatedField, self).__init__(*args, **kwargs)
+ self.allow_blank = True
+ self.allow_null = True
+
+ def to_internal_value(self, data):
+ return data
+
+ def to_representation(self, value):
+ return value
+
+
class ListField(Field):
- child = None
+ child = _UnvalidatedField()
initial = []
default_error_messages = {
'not_a_list': _('Expected a list of items but got type "{input_type}".')
@@ -1122,7 +1153,6 @@ class ListField(Field):
def __init__(self, *args, **kwargs):
self.child = kwargs.pop('child', copy.deepcopy(self.child))
- assert self.child is not None, '`child` is a required argument.'
assert not inspect.isclass(self.child), '`child` has not been instantiated.'
super(ListField, self).__init__(*args, **kwargs)
self.child.bind(field_name='', parent=self)
@@ -1151,6 +1181,49 @@ class ListField(Field):
return [self.child.to_representation(item) for item in data]
+class DictField(Field):
+ child = _UnvalidatedField()
+ initial = []
+ default_error_messages = {
+ 'not_a_dict': _('Expected a dictionary of items but got type "{input_type}".')
+ }
+
+ def __init__(self, *args, **kwargs):
+ self.child = kwargs.pop('child', copy.deepcopy(self.child))
+ assert not inspect.isclass(self.child), '`child` has not been instantiated.'
+ super(DictField, self).__init__(*args, **kwargs)
+ self.child.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 html.parse_html_list(dictionary, prefix=self.field_name)
+ return dictionary.get(self.field_name, empty)
+
+ def to_internal_value(self, data):
+ """
+ Dicts of native values <- Dicts of primitive datatypes.
+ """
+ if html.is_html_input(data):
+ data = html.parse_html_dict(data)
+ if not isinstance(data, dict):
+ self.fail('not_a_dict', input_type=type(data).__name__)
+ return dict([
+ (six.text_type(key), self.child.run_validation(value))
+ for key, value in data.items()
+ ])
+
+ def to_representation(self, value):
+ """
+ List of object instances -> List of dicts of primitive datatypes.
+ """
+ return dict([
+ (six.text_type(key), self.child.to_representation(val))
+ for key, val in value.items()
+ ])
+
+
# Miscellaneous field types...
class ReadOnlyField(Field):
diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py
index 7a51bb05..437d1339 100644
--- a/rest_framework/parsers.py
+++ b/rest_framework/parsers.py
@@ -152,7 +152,7 @@ class FileUploadParser(BaseParser):
None,
encoding)
if result is not None:
- return DataAndFiles(None, {'file': result[1]})
+ return DataAndFiles({}, {'file': result[1]})
# This is the standard case.
possible_sizes = [x.chunk_size for x in upload_handlers if x.chunk_size]
diff --git a/rest_framework/relations.py b/rest_framework/relations.py
index a85edfec..66857a41 100644
--- a/rest_framework/relations.py
+++ b/rest_framework/relations.py
@@ -338,7 +338,12 @@ class ManyRelatedField(Field):
# We override the default field access in order to support
# lists in HTML forms.
if html.is_html_input(dictionary):
+ # Don't return [] if the update is partial
+ if self.field_name not in dictionary:
+ if getattr(self.root, 'partial', False):
+ return empty
return dictionary.getlist(self.field_name)
+
return dictionary.get(self.field_name, empty)
def to_internal_value(self, data):
diff --git a/rest_framework/routers.py b/rest_framework/routers.py
index 827da034..6a4184e2 100644
--- a/rest_framework/routers.py
+++ b/rest_framework/routers.py
@@ -130,19 +130,13 @@ class SimpleRouter(BaseRouter):
If `base_name` is not specified, attempt to automatically determine
it from the viewset.
"""
- # Note that `.model` attribute on views is deprecated, although we
- # enforce the deprecation on the view `get_serializer_class()` and
- # `get_queryset()` methods, rather than here.
- model_cls = getattr(viewset, 'model', None)
queryset = getattr(viewset, 'queryset', None)
- if model_cls is None and queryset is not None:
- model_cls = queryset.model
- assert model_cls, '`base_name` argument not specified, and could ' \
+ assert queryset is not None, '`base_name` argument not specified, and could ' \
'not automatically determine the name from the viewset, as ' \
'it does not have a `.queryset` attribute.'
- return model_cls._meta.object_name.lower()
+ return queryset.model._meta.object_name.lower()
def get_routes(self, viewset):
"""
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py
index d9a67441..a3b8196b 100644
--- a/rest_framework/serializers.py
+++ b/rest_framework/serializers.py
@@ -14,7 +14,7 @@ from __future__ import unicode_literals
from django.db import models
from django.db.models.fields import FieldDoesNotExist, Field as DjangoModelField
from django.utils.translation import ugettext_lazy as _
-from rest_framework.compat import unicode_to_repr
+from rest_framework.compat import postgres_fields, unicode_to_repr
from rest_framework.utils import model_meta
from rest_framework.utils.field_mapping import (
get_url_kwargs, get_field_kwargs,
@@ -1329,6 +1329,16 @@ class ModelSerializer(Serializer):
return validators
+if hasattr(models, 'UUIDField'):
+ ModelSerializer._field_mapping[models.UUIDField] = UUIDField
+
+if postgres_fields:
+ class CharMappingField(DictField):
+ child = CharField()
+
+ ModelSerializer._field_mapping[postgres_fields.HStoreField] = CharMappingField
+
+
class HyperlinkedModelSerializer(ModelSerializer):
"""
A type of `ModelSerializer` that uses hyperlinked relationships instead
diff --git a/rest_framework/settings.py b/rest_framework/settings.py
index ca5af86e..7331f265 100644
--- a/rest_framework/settings.py
+++ b/rest_framework/settings.py
@@ -18,6 +18,7 @@ REST framework settings, checking for user settings first, then falling
back to the defaults.
"""
from __future__ import unicode_literals
+from django.test.signals import setting_changed
from django.conf import settings
from django.utils import importlib, six
from rest_framework import ISO_8601
@@ -207,3 +208,13 @@ class APISettings(object):
api_settings = APISettings(USER_SETTINGS, DEFAULTS, IMPORT_STRINGS)
+
+
+def reload_api_settings(*args, **kwargs):
+ global api_settings
+ setting, value = kwargs['setting'], kwargs['value']
+ if setting == 'REST_FRAMEWORK':
+ api_settings = APISettings(value, DEFAULTS, IMPORT_STRINGS)
+
+
+setting_changed.connect(reload_api_settings)
diff --git a/rest_framework/utils/field_mapping.py b/rest_framework/utils/field_mapping.py
index cba40d31..c97ec5d0 100644
--- a/rest_framework/utils/field_mapping.py
+++ b/rest_framework/utils/field_mapping.py
@@ -38,6 +38,9 @@ class ClassLookupDict(object):
return self.mapping[cls]
raise KeyError('Class %s not found in lookup.', cls.__name__)
+ def __setitem__(self, key, value):
+ self.mapping[key] = value
+
def needs_label(model_field, field_name):
"""
diff --git a/tests/test_fields.py b/tests/test_fields.py
index dc21c234..48ada780 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -4,6 +4,7 @@ from rest_framework import serializers
import datetime
import django
import pytest
+import uuid
# Tests for field keyword arguments and core functionality.
@@ -467,6 +468,23 @@ class TestURLField(FieldValues):
field = serializers.URLField()
+class TestUUIDField(FieldValues):
+ """
+ Valid and invalid values for `UUIDField`.
+ """
+ valid_inputs = {
+ '825d7aeb-05a9-45b5-a5b7-05df87923cda': uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'),
+ '825d7aeb05a945b5a5b705df87923cda': uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda')
+ }
+ invalid_inputs = {
+ '825d7aeb-05a9-45b5-a5b7': ['"825d7aeb-05a9-45b5-a5b7" is not a valid UUID.']
+ }
+ outputs = {
+ uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'): '825d7aeb-05a9-45b5-a5b7-05df87923cda'
+ }
+ field = serializers.UUIDField()
+
+
# Number types...
class TestIntegerField(FieldValues):
@@ -1029,7 +1047,7 @@ class TestValidImageField(FieldValues):
class TestListField(FieldValues):
"""
- Values for `ListField`.
+ Values for `ListField` with IntegerField as child.
"""
valid_inputs = [
([1, 2, 3], [1, 2, 3]),
@@ -1046,6 +1064,55 @@ class TestListField(FieldValues):
field = serializers.ListField(child=serializers.IntegerField())
+class TestUnvalidatedListField(FieldValues):
+ """
+ Values for `ListField` with no `child` argument.
+ """
+ valid_inputs = [
+ ([1, '2', True, [4, 5, 6]], [1, '2', True, [4, 5, 6]]),
+ ]
+ invalid_inputs = [
+ ('not a list', ['Expected a list of items but got type "str".']),
+ ]
+ outputs = [
+ ([1, '2', True, [4, 5, 6]], [1, '2', True, [4, 5, 6]]),
+ ]
+ field = serializers.ListField()
+
+
+class TestDictField(FieldValues):
+ """
+ Values for `ListField` with CharField as child.
+ """
+ valid_inputs = [
+ ({'a': 1, 'b': '2', 3: 3}, {'a': '1', 'b': '2', '3': '3'}),
+ ]
+ invalid_inputs = [
+ ({'a': 1, 'b': None}, ['This field may not be null.']),
+ ('not a dict', ['Expected a dictionary of items but got type "str".']),
+ ]
+ outputs = [
+ ({'a': 1, 'b': '2', 3: 3}, {'a': '1', 'b': '2', '3': '3'}),
+ ]
+ field = serializers.DictField(child=serializers.CharField())
+
+
+class TestUnvalidatedDictField(FieldValues):
+ """
+ Values for `ListField` with no `child` argument.
+ """
+ valid_inputs = [
+ ({'a': 1, 'b': [4, 5, 6], 1: 123}, {'a': 1, 'b': [4, 5, 6], '1': 123}),
+ ]
+ invalid_inputs = [
+ ('not a dict', ['Expected a dictionary of items but got type "str".']),
+ ]
+ outputs = [
+ ({'a': 1, 'b': [4, 5, 6]}, {'a': 1, 'b': [4, 5, 6]}),
+ ]
+ field = serializers.DictField()
+
+
# Tests for FieldField.
# ---------------------
diff --git a/tests/test_filters.py b/tests/test_filters.py
index 5b1b6ca5..355f02ce 100644
--- a/tests/test_filters.py
+++ b/tests/test_filters.py
@@ -5,13 +5,15 @@ from django.db import models
from django.conf.urls import patterns, url
from django.core.urlresolvers import reverse
from django.test import TestCase
+from django.test.utils import override_settings
from django.utils import unittest
from django.utils.dateparse import parse_date
+from django.utils.six.moves import reload_module
from rest_framework import generics, serializers, status, filters
from rest_framework.compat import django_filters
from rest_framework.test import APIRequestFactory
from .models import BaseFilterableItem, FilterableItem, BasicModel
-from .utils import temporary_setting
+
factory = APIRequestFactory()
@@ -404,7 +406,9 @@ class SearchFilterTests(TestCase):
)
def test_search_with_nonstandard_search_param(self):
- with temporary_setting('SEARCH_PARAM', 'query', module=filters):
+ with override_settings(REST_FRAMEWORK={'SEARCH_PARAM': 'query'}):
+ reload_module(filters)
+
class SearchListView(generics.ListAPIView):
queryset = SearchFilterModel.objects.all()
serializer_class = SearchFilterSerializer
@@ -422,6 +426,8 @@ class SearchFilterTests(TestCase):
]
)
+ reload_module(filters)
+
class OrderingFilterModel(models.Model):
title = models.CharField(max_length=20)
@@ -642,7 +648,9 @@ class OrderingFilterTests(TestCase):
)
def test_ordering_with_nonstandard_ordering_param(self):
- with temporary_setting('ORDERING_PARAM', 'order', filters):
+ with override_settings(REST_FRAMEWORK={'ORDERING_PARAM': 'order'}):
+ reload_module(filters)
+
class OrderingListView(generics.ListAPIView):
queryset = OrderingFilterModel.objects.all()
serializer_class = OrderingFilterSerializer
@@ -662,6 +670,8 @@ class OrderingFilterTests(TestCase):
]
)
+ reload_module(filters)
+
class SensitiveOrderingFilterModel(models.Model):
username = models.CharField(max_length=20)
diff --git a/tests/test_relations.py b/tests/test_relations.py
index 08c92242..fbe176e2 100644
--- a/tests/test_relations.py
+++ b/tests/test_relations.py
@@ -1,6 +1,8 @@
from .utils import mock_reverse, fail_reverse, BadType, MockObject, MockQueryset
from django.core.exceptions import ImproperlyConfigured
+from django.utils.datastructures import MultiValueDict
from rest_framework import serializers
+from rest_framework.fields import empty
from rest_framework.test import APISimpleTestCase
import pytest
@@ -134,3 +136,34 @@ class TestSlugRelatedField(APISimpleTestCase):
def test_representation(self):
representation = self.field.to_representation(self.instance)
assert representation == self.instance.name
+
+
+class TestManyRelatedField(APISimpleTestCase):
+ def setUp(self):
+ self.instance = MockObject(pk=1, name='foo')
+ self.field = serializers.StringRelatedField(many=True)
+ self.field.field_name = 'foo'
+
+ def test_get_value_regular_dictionary_full(self):
+ assert 'bar' == self.field.get_value({'foo': 'bar'})
+ assert empty == self.field.get_value({'baz': 'bar'})
+
+ def test_get_value_regular_dictionary_partial(self):
+ setattr(self.field.root, 'partial', True)
+ assert 'bar' == self.field.get_value({'foo': 'bar'})
+ assert empty == self.field.get_value({'baz': 'bar'})
+
+ def test_get_value_multi_dictionary_full(self):
+ mvd = MultiValueDict({'foo': ['bar1', 'bar2']})
+ assert ['bar1', 'bar2'] == self.field.get_value(mvd)
+
+ mvd = MultiValueDict({'baz': ['bar1', 'bar2']})
+ assert [] == self.field.get_value(mvd)
+
+ def test_get_value_multi_dictionary_partial(self):
+ setattr(self.field.root, 'partial', True)
+ mvd = MultiValueDict({'foo': ['bar1', 'bar2']})
+ assert ['bar1', 'bar2'] == self.field.get_value(mvd)
+
+ mvd = MultiValueDict({'baz': ['bar1', 'bar2']})
+ assert empty == self.field.get_value(mvd)
diff --git a/tests/test_routers.py b/tests/test_routers.py
index 86113f5d..948c69bb 100644
--- a/tests/test_routers.py
+++ b/tests/test_routers.py
@@ -180,7 +180,7 @@ class TestLookupValueRegex(TestCase):
class TestTrailingSlashIncluded(TestCase):
def setUp(self):
class NoteViewSet(viewsets.ModelViewSet):
- model = RouterTestModel
+ queryset = RouterTestModel.objects.all()
self.router = SimpleRouter()
self.router.register(r'notes', NoteViewSet)
@@ -195,7 +195,7 @@ class TestTrailingSlashIncluded(TestCase):
class TestTrailingSlashRemoved(TestCase):
def setUp(self):
class NoteViewSet(viewsets.ModelViewSet):
- model = RouterTestModel
+ queryset = RouterTestModel.objects.all()
self.router = SimpleRouter(trailing_slash=False)
self.router.register(r'notes', NoteViewSet)
@@ -210,7 +210,8 @@ class TestTrailingSlashRemoved(TestCase):
class TestNameableRoot(TestCase):
def setUp(self):
class NoteViewSet(viewsets.ModelViewSet):
- model = RouterTestModel
+ queryset = RouterTestModel.objects.all()
+
self.router = DefaultRouter()
self.router.root_view_name = 'nameable-root'
self.router.register(r'notes', NoteViewSet)
diff --git a/tests/utils.py b/tests/utils.py
index 5e902ba9..5b2d7586 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -1,30 +1,5 @@
-from contextlib import contextmanager
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import NoReverseMatch
-from django.utils import six
-from rest_framework.settings import api_settings
-
-
-@contextmanager
-def temporary_setting(setting, value, module=None):
- """
- Temporarily change value of setting for test.
-
- Optionally reload given module, useful when module uses value of setting on
- import.
- """
- original_value = getattr(api_settings, setting)
- setattr(api_settings, setting, value)
-
- if module is not None:
- six.moves.reload_module(module)
-
- yield
-
- setattr(api_settings, setting, original_value)
-
- if module is not None:
- six.moves.reload_module(module)
class MockObject(object):
diff --git a/tox.ini b/tox.ini
index b4b708be..129752f3 100644
--- a/tox.ini
+++ b/tox.ini
@@ -18,7 +18,7 @@ deps =
djangomaster: https://github.com/django/django/zipball/master
django-guardian==1.2.4
pytest-django==2.8.0
- {py26,py27,py32,py33,py34}-django{14,15,16,17}: django-filter==0.9.1
+ django-filter==0.9.2
markdown>=2.1.0
[testenv:py27-flake8]