aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/resources.py
diff options
context:
space:
mode:
authorTom Christie2011-05-27 09:58:21 +0100
committerTom Christie2011-05-27 09:58:21 +0100
commitcefc6a25c239411f17740742a121a1045563d90c (patch)
tree00fd277dbc2e810ace38bc9f7846dc6baeaa2390 /djangorestframework/resources.py
parent894bf34451d0b3ff73f75e3ea954088e9078bed8 (diff)
downloaddjango-rest-framework-cefc6a25c239411f17740742a121a1045563d90c.tar.bz2
forms/models can be set on the view as well as the resource
Diffstat (limited to 'djangorestframework/resources.py')
-rw-r--r--djangorestframework/resources.py30
1 files changed, 28 insertions, 2 deletions
diff --git a/djangorestframework/resources.py b/djangorestframework/resources.py
index 27d25ba9..de8e1351 100644
--- a/djangorestframework/resources.py
+++ b/djangorestframework/resources.py
@@ -165,11 +165,22 @@ class FormResource(Resource):
On calling :meth:`validate_request` this validator may set a :attr:`bound_form_instance` attribute on the
view, which may be used by some renderers.
"""
- form = None
+
"""
The :class:`Form` class that should be used for request validation.
+ This can be overridden by a :attr:`form` attribute on the :class:`.View`.
"""
-
+ form = None
+
+ def __init__(self, view):
+ """
+ Allow a :attr:`form` attributes set on the :class:`View` to override
+ the :attr:`form` attribute set on the :class:`Resource`.
+ """
+ super(FormResource, self).__init__(view)
+
+ if getattr(view, 'form', None):
+ self.form = view.form
def validate_request(self, data, files):
@@ -314,11 +325,15 @@ class ModelResource(FormResource):
"""
The form class that should be used for request validation.
If set to :const:`None` then the default model form validation will be used.
+
+ This can be overridden by a :attr:`form` attribute on the :class:`.View`.
"""
form = None
"""
The model class which this resource maps to.
+
+ This can be overridden by a :attr:`model` attribute on the :class:`.View`.
"""
model = None
@@ -346,6 +361,17 @@ class ModelResource(FormResource):
include = ('url',)
+ def __init__(self, view):
+ """
+ Allow :attr:`form` and :attr:`model` attributes set on the
+ :class:`View` to override the :attr:`form` and :attr:`model`
+ attributes set on the :class:`Resource`.
+ """
+ super(ModelResource, self).__init__(view)
+
+ if getattr(view, 'model', None):
+ self.model = view.model
+
def validate_request(self, data, files):
"""
Given some content as input return some cleaned, validated content.