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
0cfe2acd
Commit
0cfe2acd
authored
Jan 11, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor cleanup on ModelMixin
parent
dacc9c0f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
21 deletions
+26
-21
djangorestframework/mixins.py
+26
-21
No files found.
djangorestframework/mixins.py
View file @
0cfe2acd
...
@@ -490,10 +490,9 @@ class ModelMixin(object):
...
@@ -490,10 +490,9 @@ class ModelMixin(object):
If a *ModelMixin is going to retrive an instance (or queryset) using args and kwargs
If a *ModelMixin is going to retrive an instance (or queryset) using args and kwargs
passed by as URL arguments, it should provied arguments to objects.get and objects.filter
passed by as URL arguments, it should provied arguments to objects.get and objects.filter
methods wrapped in by `build_query`
methods wrapped in by `build_query`
If a *ModelMixin is going to create/update an instance get_instance_data handles the instance
data creation/preaparation.
If a *ModelMixin is going to create/update an instance get_instance_data
handles the instance data creation/preaparation.
"""
"""
def
build_query
(
self
,
*
args
,
**
kwargs
):
def
build_query
(
self
,
*
args
,
**
kwargs
):
...
@@ -508,24 +507,32 @@ class ModelMixin(object):
...
@@ -508,24 +507,32 @@ class ModelMixin(object):
used for the objects retrival with filter/get queryset methods.
used for the objects retrival with filter/get queryset methods.
Technically, neither args nor kwargs have to be provided, however the default
Technically, neither args nor kwargs have to be provided, however the default
behaviour is to map all kwargs as the query constructors so that if this
behaviour is to map all kwargs as the query constructors so that if this
method is not overriden only kwargs keys being model fields are valid.
method is not overriden only kwargs keys being model fields are valid.
If args are provided, the last one (args[-1) is understood as instance pk. This
should be removed in the future, though.
If positional args are provided, the last one argument is understood
as the primary key. However this usage should be considered
deperecated, and will be removed in a future version.
"""
"""
tmp
=
dict
(
kwargs
)
tmp
=
dict
(
kwargs
)
# If the URLconf includes a .(?P<format>\w+) pattern to match against
# a .json, .xml suffix, then drop the 'format' kwarg before
# constructing the query.
if
BaseRenderer
.
_FORMAT_QUERY_PARAM
in
tmp
:
del
tmp
[
BaseRenderer
.
_FORMAT_QUERY_PARAM
]
if
args
:
if
args
:
# If we have any none kwargs then assume the last represents the primrary key
# If we have any no kwargs then assume the last arg represents the
# Otherwise assume the kwargs uniquely identify the model
# primrary key. Otherwise assume the kwargs uniquely identify the
# model.
tmp
.
update
({
'pk'
:
args
[
-
1
]})
tmp
.
update
({
'pk'
:
args
[
-
1
]})
return
Q
(
**
tmp
)
return
Q
(
**
tmp
)
def
get_instance_data
(
self
,
model
,
content
,
**
kwargs
):
def
get_instance_data
(
self
,
model
,
content
,
**
kwargs
):
""" Returns the dict with the data for model instance creation/update query.
"""
Returns the dict with the data for model instance creation/update query.
Arguments:
Arguments:
- model: model class (django.db.models.Model subclass) to work with
- model: model class (django.db.models.Model subclass) to work with
...
@@ -533,15 +540,15 @@ class ModelMixin(object):
...
@@ -533,15 +540,15 @@ class ModelMixin(object):
- kwargs: a dict of URL provided keyword arguments
- kwargs: a dict of URL provided keyword arguments
The create/update queries are created basicly with the contet provided
The create/update queries are created basicly with the contet provided
with POST/PUT HTML methods and kwargs passed in the URL. This methods
simply merges
with POST/PUT HTML methods and kwargs passed in the URL. This methods
the URL data and the content preaparing the ready-to-use data dictionary.
simply merges the URL data and the content preaparing the ready-to-use
data dictionary.
"""
"""
tmp
=
dict
(
kwargs
)
tmp
=
dict
(
kwargs
)
for
field
in
model
.
_meta
.
fields
:
for
field
in
model
.
_meta
.
fields
:
if
isinstance
(
field
,
ForeignKey
)
and
tmp
.
has_key
(
field
.
name
)
:
if
isinstance
(
field
,
ForeignKey
)
and
field
.
name
in
tmp
:
# translate 'related_field' kwargs into 'related_field_id'
# translate 'related_field' kwargs into 'related_field_id'
tmp
[
field
.
name
+
'_id'
]
=
tmp
[
field
.
name
]
tmp
[
field
.
name
+
'_id'
]
=
tmp
[
field
.
name
]
del
tmp
[
field
.
name
]
del
tmp
[
field
.
name
]
...
@@ -569,10 +576,10 @@ class ReadModelMixin(ModelMixin):
...
@@ -569,10 +576,10 @@ class ReadModelMixin(ModelMixin):
# Build query is overriden to filter the kwargs priori
# Build query is overriden to filter the kwargs priori
# to use them as build_query argument
# to use them as build_query argument
filtered_keywords
=
kwargs
.
copy
()
filtered_keywords
=
kwargs
.
copy
()
if
BaseRenderer
.
_FORMAT_QUERY_PARAM
in
filtered_keywords
:
del
filtered_keywords
[
BaseRenderer
.
_FORMAT_QUERY_PARAM
]
return
super
(
ReadModelMixin
,
self
)
.
build_query
(
*
args
,
**
filtered_keywords
)
return
super
(
ReadModelMixin
,
self
)
.
build_query
(
*
args
,
**
filtered_keywords
)
class
CreateModelMixin
(
ModelMixin
):
class
CreateModelMixin
(
ModelMixin
):
"""
"""
Behavior to create a `model` instance on POST requests
Behavior to create a `model` instance on POST requests
...
@@ -584,15 +591,13 @@ class CreateModelMixin(ModelMixin):
...
@@ -584,15 +591,13 @@ class CreateModelMixin(ModelMixin):
content
=
dict
(
self
.
CONTENT
)
content
=
dict
(
self
.
CONTENT
)
m2m_data
=
{}
m2m_data
=
{}
for
field
in
model
.
_meta
.
many_to_many
:
for
field
in
model
.
_meta
.
many_to_many
:
if
content
.
has_key
(
field
.
name
)
:
if
field
.
name
in
content
:
m2m_data
[
field
.
name
]
=
(
m2m_data
[
field
.
name
]
=
(
field
.
m2m_reverse_field_name
(),
content
[
field
.
name
]
field
.
m2m_reverse_field_name
(),
content
[
field
.
name
]
)
)
del
content
[
field
.
name
]
del
content
[
field
.
name
]
instance
=
model
(
**
self
.
get_instance_data
(
model
,
content
,
*
args
,
**
kwargs
))
instance
=
model
(
**
self
.
get_instance_data
(
model
,
content
,
*
args
,
**
kwargs
))
instance
.
save
()
instance
.
save
()
...
...
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