Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
ca4b7560
Commit
ca4b7560
authored
Jul 01, 2011
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
merge
parents
16d8c325
dc9960f7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
31 deletions
+39
-31
djangorestframework/mixins.py
+10
-10
djangorestframework/resources.py
+29
-21
No files found.
djangorestframework/mixins.py
View file @
ca4b7560
...
@@ -491,17 +491,17 @@ class ReadModelMixin(object):
...
@@ -491,17 +491,17 @@ class ReadModelMixin(object):
try
:
try
:
if
args
:
if
args
:
# If we have any none kwargs then assume the last represents the primrary key
# If we have any none kwargs then assume the last represents the primrary key
instance
=
model
.
objects
.
get
(
pk
=
args
[
-
1
],
**
kwargs
)
self
.
model_
instance
=
model
.
objects
.
get
(
pk
=
args
[
-
1
],
**
kwargs
)
else
:
else
:
# Otherwise assume the kwargs uniquely identify the model
# Otherwise assume the kwargs uniquely identify the model
filtered_keywords
=
kwargs
.
copy
()
filtered_keywords
=
kwargs
.
copy
()
if
BaseRenderer
.
_FORMAT_QUERY_PARAM
in
filtered_keywords
:
if
BaseRenderer
.
_FORMAT_QUERY_PARAM
in
filtered_keywords
:
del
filtered_keywords
[
BaseRenderer
.
_FORMAT_QUERY_PARAM
]
del
filtered_keywords
[
BaseRenderer
.
_FORMAT_QUERY_PARAM
]
instance
=
model
.
objects
.
get
(
**
filtered_keywords
)
self
.
model_
instance
=
model
.
objects
.
get
(
**
filtered_keywords
)
except
model
.
DoesNotExist
:
except
model
.
DoesNotExist
:
raise
ErrorResponse
(
status
.
HTTP_404_NOT_FOUND
)
raise
ErrorResponse
(
status
.
HTTP_404_NOT_FOUND
)
return
instance
return
self
.
model_
instance
class
CreateModelMixin
(
object
):
class
CreateModelMixin
(
object
):
...
@@ -540,19 +540,19 @@ class UpdateModelMixin(object):
...
@@ -540,19 +540,19 @@ class UpdateModelMixin(object):
try
:
try
:
if
args
:
if
args
:
# If we have any none kwargs then assume the last represents the primrary key
# If we have any none kwargs then assume the last represents the primrary key
instance
=
model
.
objects
.
get
(
pk
=
args
[
-
1
],
**
kwargs
)
self
.
model_
instance
=
model
.
objects
.
get
(
pk
=
args
[
-
1
],
**
kwargs
)
else
:
else
:
# Otherwise assume the kwargs uniquely identify the model
# Otherwise assume the kwargs uniquely identify the model
instance
=
model
.
objects
.
get
(
**
kwargs
)
self
.
model_
instance
=
model
.
objects
.
get
(
**
kwargs
)
for
(
key
,
val
)
in
self
.
CONTENT
.
items
():
for
(
key
,
val
)
in
self
.
CONTENT
.
items
():
setattr
(
instance
,
key
,
val
)
setattr
(
self
.
model_
instance
,
key
,
val
)
except
model
.
DoesNotExist
:
except
model
.
DoesNotExist
:
instance
=
model
(
**
self
.
CONTENT
)
self
.
model_
instance
=
model
(
**
self
.
CONTENT
)
instance
.
save
()
self
.
model_
instance
.
save
()
instance
.
save
()
self
.
model_
instance
.
save
()
return
instance
return
self
.
model_
instance
class
DeleteModelMixin
(
object
):
class
DeleteModelMixin
(
object
):
...
...
djangorestframework/resources.py
View file @
ca4b7560
...
@@ -177,14 +177,12 @@ class FormResource(Resource):
...
@@ -177,14 +177,12 @@ class FormResource(Resource):
# Return HTTP 400 response (BAD REQUEST)
# Return HTTP 400 response (BAD REQUEST)
raise
ErrorResponse
(
400
,
detail
)
raise
ErrorResponse
(
400
,
detail
)
def
get_bound_form
(
self
,
data
=
None
,
files
=
None
,
method
=
None
):
def
get_form_class
(
self
,
method
=
None
):
"""
"""
Given some content return a Django form bound to that content.
Returns the form class used to validate this resource.
If form validation is turned off (:attr:`form` class attribute is :const:`None`) then returns :const:`None`.
"""
"""
# A form on the view overrides a form on the resource.
# A form on the view overrides a form on the resource.
form
=
getattr
(
self
.
view
,
'form'
,
None
)
or
self
.
form
form
=
getattr
(
self
.
view
,
'form'
,
None
)
or
self
.
form
...
@@ -200,6 +198,16 @@ class FormResource(Resource):
...
@@ -200,6 +198,16 @@ class FormResource(Resource):
form
=
getattr
(
self
,
'
%
s_form'
%
method
.
lower
(),
form
)
form
=
getattr
(
self
,
'
%
s_form'
%
method
.
lower
(),
form
)
form
=
getattr
(
self
.
view
,
'
%
s_form'
%
method
.
lower
(),
form
)
form
=
getattr
(
self
.
view
,
'
%
s_form'
%
method
.
lower
(),
form
)
return
form
def
get_bound_form
(
self
,
data
=
None
,
files
=
None
,
method
=
None
):
"""
Given some content return a Django form bound to that content.
If form validation is turned off (:attr:`form` class attribute is :const:`None`) then returns :const:`None`.
"""
form
=
self
.
get_form_class
(
method
)
if
not
form
:
if
not
form
:
return
None
return
None
...
@@ -306,31 +314,31 @@ class ModelResource(FormResource):
...
@@ -306,31 +314,31 @@ class ModelResource(FormResource):
If the :attr:`form` class attribute has been explicitly set then that class will be used
If the :attr:`form` class attribute has been explicitly set then that class will be used
to create the Form, otherwise the model will be used to create a ModelForm.
to create the Form, otherwise the model will be used to create a ModelForm.
"""
"""
form
=
self
.
get_form_class
(
method
)
form
=
super
(
ModelResource
,
self
)
.
get_bound_form
(
data
,
files
,
method
=
method
)
if
not
form
and
self
.
model
:
# Use an explict Form if it exists
if
form
:
return
form
elif
self
.
model
:
# Fall back to ModelForm which we create on the fly
# Fall back to ModelForm which we create on the fly
class
OnTheFlyModelForm
(
forms
.
ModelForm
):
class
OnTheFlyModelForm
(
forms
.
ModelForm
):
class
Meta
:
class
Meta
:
model
=
self
.
model
model
=
self
.
model
#fields = tuple(self._model_fields_set)
#fields = tuple(self._model_fields_set)
# Instantiate the ModelForm as appropriate
form
=
OnTheFlyModelForm
if
data
and
isinstance
(
data
,
models
.
Model
):
# Bound to an existing model instance
return
OnTheFlyModelForm
(
instance
=
content
)
elif
data
is
not
None
:
return
OnTheFlyModelForm
(
data
,
files
)
return
OnTheFlyModelForm
()
# Both form and model not set? Okay bruv, whatevs...
# Both form and model not set? Okay bruv, whatevs...
return
None
if
not
form
:
return
None
# Instantiate the ModelForm as appropriate
if
data
is
not
None
or
files
is
not
None
:
if
issubclass
(
form
,
forms
.
ModelForm
)
and
hasattr
(
self
.
view
,
'model_instance'
):
# Bound to an existing model instance
return
form
(
data
,
files
,
instance
=
self
.
view
.
model_instance
)
else
:
return
form
(
data
,
files
)
return
form
()
def
url
(
self
,
instance
):
def
url
(
self
,
instance
):
"""
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment