Commit e63dcab8 by Tom Christie

Fix for rendering select templates on relationships

parent 88609ba3
...@@ -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):
......
...@@ -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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment