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
a634d10c
Commit
a634d10c
authored
Jul 05, 2011
by
Fernando Zunino
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support for creating objects with m2m relations
parent
2dc042f0
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
7 deletions
+21
-7
djangorestframework/mixins.py
+21
-7
No files found.
djangorestframework/mixins.py
View file @
a634d10c
...
...
@@ -5,7 +5,7 @@ classes that can be added to a `View`.
from
django.contrib.auth.models
import
AnonymousUser
from
django.db.models.query
import
QuerySet
from
django.db.models.fields.related
import
RelatedField
from
django.db.models.fields.related
import
ForeignKey
from
django.http
import
HttpResponse
from
django.http.multipartparser
import
LimitBytes
...
...
@@ -511,18 +511,32 @@ class CreateModelMixin(object):
def
post
(
self
,
request
,
*
args
,
**
kwargs
):
model
=
self
.
resource
.
model
# translated 'related_field' kwargs into 'related_field_id'
for
related_name
in
[
field
.
name
for
field
in
model
.
_meta
.
fields
if
isinstance
(
field
,
RelatedField
)]:
if
kwargs
.
has_key
(
related_name
):
kwargs
[
related_name
+
'_id'
]
=
kwargs
[
related_name
]
del
kwargs
[
related_name
]
# Copy the dict to keep self.CONTENT intact
content
=
dict
(
self
.
CONTENT
)
m2m_data
=
{}
for
field
in
model
.
_meta
.
fields
:
if
isinstance
(
field
,
ForeignKey
)
and
kwargs
.
has_key
(
field
.
name
):
# translate 'related_field' kwargs into 'related_field_id'
kwargs
[
field
.
name
+
'_id'
]
=
kwargs
[
field
.
name
]
del
kwargs
[
field
.
name
]
for
field
in
model
.
_meta
.
many_to_many
:
if
content
.
has_key
(
field
.
name
):
m2m_data
[
field
.
name
]
=
content
[
field
.
name
]
del
content
[
field
.
name
]
all_kw_args
=
dict
(
content
.
items
()
+
kwargs
.
items
())
all_kw_args
=
dict
(
self
.
CONTENT
.
items
()
+
kwargs
.
items
())
if
args
:
instance
=
model
(
pk
=
args
[
-
1
],
**
all_kw_args
)
else
:
instance
=
model
(
**
all_kw_args
)
instance
.
save
()
for
fieldname
in
m2m_data
:
getattr
(
instance
,
fieldname
)
.
add
(
*
m2m_data
[
fieldname
])
headers
=
{}
if
hasattr
(
instance
,
'get_absolute_url'
):
headers
[
'Location'
]
=
self
.
resource
(
self
)
.
url
(
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