Skip to content

Commit

Permalink
Fix for rendering select templates on relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Aug 7, 2015
1 parent 88609ba commit e63dcab
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
61 changes: 34 additions & 27 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,39 @@ def flatten_choices_dict(choices):
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):
"""
This class may be used to provide default values that are only used
Expand Down Expand Up @@ -1190,33 +1223,7 @@ def iter_options(self):
"""
Helper method for use with templates rendering select widgets.
"""
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 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)
return iter_options(self.grouped_choices)


class MultipleChoiceField(ChoiceField):
Expand Down
16 changes: 15 additions & 1 deletion rest_framework/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from rest_framework.compat import OrderedDict
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.utils import html
Expand Down Expand Up @@ -153,6 +153,13 @@ def choices(self):
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):
"""
Expand Down Expand Up @@ -453,3 +460,10 @@ def to_representation(self, iterable):
@property
def choices(self):
return self.child_relation.choices

@property
def grouped_choices(self):
return self.choices

def iter_options(self):
return iter_options(self.grouped_choices)

0 comments on commit e63dcab

Please sign in to comment.