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
e63dcab8
Commit
e63dcab8
authored
Aug 07, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix for rendering select templates on relationships
parent
88609ba3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
28 deletions
+49
-28
rest_framework/fields.py
+34
-27
rest_framework/relations.py
+15
-1
No files found.
rest_framework/fields.py
View file @
e63dcab8
...
@@ -155,6 +155,39 @@ def flatten_choices_dict(choices):
...
@@ -155,6 +155,39 @@ def flatten_choices_dict(choices):
return
ret
return
ret
def
iter_options
(
grouped_choices
):
"""
Helper function for options and option groups in templates.
"""
class
StartOptionGroup
(
object
):
start_option_group
=
True
end_option_group
=
False
def
__init__
(
self
,
label
):
self
.
label
=
label
class
EndOptionGroup
(
object
):
start_option_group
=
False
end_option_group
=
True
class
Option
(
object
):
start_option_group
=
False
end_option_group
=
False
def
__init__
(
self
,
value
,
display_text
):
self
.
value
=
value
self
.
display_text
=
display_text
for
key
,
value
in
grouped_choices
.
items
():
if
isinstance
(
value
,
dict
):
yield
StartOptionGroup
(
label
=
key
)
for
sub_key
,
sub_value
in
value
.
items
():
yield
Option
(
value
=
sub_key
,
display_text
=
sub_value
)
yield
EndOptionGroup
()
else
:
yield
Option
(
value
=
key
,
display_text
=
value
)
class
CreateOnlyDefault
(
object
):
class
CreateOnlyDefault
(
object
):
"""
"""
This class may be used to provide default values that are only used
This class may be used to provide default values that are only used
...
@@ -1190,33 +1223,7 @@ class ChoiceField(Field):
...
@@ -1190,33 +1223,7 @@ class ChoiceField(Field):
"""
"""
Helper method for use with templates rendering select widgets.
Helper method for use with templates rendering select widgets.
"""
"""
class
StartOptionGroup
(
object
):
return
iter_options
(
self
.
grouped_choices
)
start_option_group
=
True
end_option_group
=
False
def
__init__
(
self
,
label
):
self
.
label
=
label
class
EndOptionGroup
(
object
):
start_option_group
=
False
end_option_group
=
True
class
Option
(
object
):
start_option_group
=
False
end_option_group
=
False
def
__init__
(
self
,
value
,
display_text
):
self
.
value
=
value
self
.
display_text
=
display_text
for
key
,
value
in
self
.
grouped_choices
.
items
():
if
isinstance
(
value
,
dict
):
yield
StartOptionGroup
(
label
=
key
)
for
sub_key
,
sub_value
in
value
.
items
():
yield
Option
(
value
=
sub_key
,
display_text
=
sub_value
)
yield
EndOptionGroup
()
else
:
yield
Option
(
value
=
key
,
display_text
=
value
)
class
MultipleChoiceField
(
ChoiceField
):
class
MultipleChoiceField
(
ChoiceField
):
...
...
rest_framework/relations.py
View file @
e63dcab8
...
@@ -14,7 +14,7 @@ from django.utils.translation import ugettext_lazy as _
...
@@ -14,7 +14,7 @@ from django.utils.translation import ugettext_lazy as _
from
rest_framework.compat
import
OrderedDict
from
rest_framework.compat
import
OrderedDict
from
rest_framework.fields
import
(
from
rest_framework.fields
import
(
Field
,
empty
,
get_attribute
,
is_simple_callable
Field
,
empty
,
get_attribute
,
is_simple_callable
,
iter_options
)
)
from
rest_framework.reverse
import
reverse
from
rest_framework.reverse
import
reverse
from
rest_framework.utils
import
html
from
rest_framework.utils
import
html
...
@@ -153,6 +153,13 @@ class RelatedField(Field):
...
@@ -153,6 +153,13 @@ class RelatedField(Field):
for
item
in
queryset
for
item
in
queryset
])
])
@property
def
grouped_choices
(
self
):
return
self
.
choices
def
iter_options
(
self
):
return
iter_options
(
self
.
grouped_choices
)
class
StringRelatedField
(
RelatedField
):
class
StringRelatedField
(
RelatedField
):
"""
"""
...
@@ -453,3 +460,10 @@ class ManyRelatedField(Field):
...
@@ -453,3 +460,10 @@ class ManyRelatedField(Field):
@property
@property
def
choices
(
self
):
def
choices
(
self
):
return
self
.
child_relation
.
choices
return
self
.
child_relation
.
choices
@property
def
grouped_choices
(
self
):
return
self
.
choices
def
iter_options
(
self
):
return
iter_options
(
self
.
grouped_choices
)
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