More forms support

This commit is contained in:
Tom Christie 2014-10-01 21:35:27 +01:00
parent c171fa21ac
commit ffc6aa3abc
4 changed files with 45 additions and 4 deletions

View File

@ -38,6 +38,16 @@ class RelatedField(Field):
queryset = queryset.all()
return queryset
@property
def choices(self):
return dict([
(
str(self.to_representation(item)),
str(item)
)
for item in self.queryset.all()
])
class StringRelatedField(Field):
"""
@ -255,3 +265,13 @@ class ManyRelation(Field):
self.child_relation.to_representation(value)
for value in obj.all()
]
@property
def choices(self):
return dict([
(
str(self.child_relation.to_representation(item)),
str(item)
)
for item in self.child_relation.queryset.all()
])

View File

@ -360,22 +360,43 @@ class HTMLFormRenderer(BaseRenderer):
serializers.MultipleChoiceField: {
'default': 'select_multiple.html',
'checkbox': 'select_checkbox.html'
},
serializers.ManyRelation: {
'default': 'select_multiple.html',
'checkbox': 'select_checkbox.html'
}
})
input_type = ClassLookupDict({
serializers.Field: 'text',
serializers.EmailField: 'email',
serializers.URLField: 'url',
serializers.IntegerField: 'number',
serializers.DateTimeField: 'datetime-local',
serializers.DateField: 'date',
serializers.TimeField: 'time',
})
def render_field(self, field, value, errors, layout=None):
layout = layout or 'vertical'
style_type = field.style.get('type', 'default')
if style_type == 'textarea' and layout == 'inline':
style_type = 'default'
input_type = self.input_type[field]
if input_type == 'datetime-local':
value = value.rstrip('Z')
base = self.field_templates[field][style_type]
template_name = 'rest_framework/fields/' + layout + '/' + base
template = loader.get_template(template_name)
context = Context({
'field': field,
'value': value,
'errors': errors
'errors': errors,
'input_type': input_type
})
return template.render(context)
def render(self, data, accepted_media_type=None, renderer_context=None):
@ -388,7 +409,7 @@ class HTMLFormRenderer(BaseRenderer):
template = loader.get_template(self.template)
context = RequestContext(request, {
'form': data,
'layout': getattr(getattr(data, 'Meta', None), 'layout', 'vertical'),
'layout': getattr(getattr(data, 'Meta', None), 'layout', 'horizontal'),
'renderer': self
})
return template.render(context)

View File

@ -1,7 +1,7 @@
<div class="form-group">
{% include "rest_framework/fields/vertical/label.html" %}
<select multiple class="form-control" name="{{ field.field_name }}">
{% for key, text in field.choices.items() %}
{% for key, text in field.choices.items %}
<option value="{{ key }}" {% if key in value %}selected{% endif %}>{{ text }}</option>
{% endfor %}
</select>

View File

@ -9,7 +9,7 @@
<div class="well">
{% load rest_framework %}
<form {% if layout == "inline" %}class="form-inline"{% elif layout == "horizontal" %}class="form-horizontal"{% endif %} role="form" action="" method="POST">
<form {% if layout == "inline" %}class="form-inline"{% elif layout == "horizontal" %}class="form-horizontal"{% endif %} role="form" action="." method="POST">
{% csrf_token %}
{% for field, value, errors in form %}
{% render_field field value errors layout=layout renderer=renderer %}