とを使用MultiValueField
しMultiWidget
て、この動作を取得できます。compress
とメソッドについて考えて変更する必要がありますがdecompress
、次のようなもので途中まで進むことができます。
from django import forms
from crispy_forms.helper import FormHelper
class MyWidget(forms.MultiWidget):
widgets = (forms.TextInput(), forms.TextInput())
super(ExpirationDateWidget, self).__init__(widgets)
def decompress(self, value):
if value:
return value.split('|') # or however you combined the two values
return [None, None]
class MyMultiField(forms.MultiValueField):
def __init__(self, *args, **kwargs):
self.widget = MyWidget(*args, **kwargs)
def compress(self, data_list):
return data_list[0] + '|' + data_list[1] # or other logical compression
class MyForm(forms.Form):
multi_field = MyMultiField()
# other fields like category and category_new
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.layout = Layout('multi_field', 'category', 'category_new')
これにより、次のようになります。
<div id="div_id_multi_field" class="control-group">
<label for="id_multi_field_0" class="control-label">Multi Field</label>
<div class="controls">
<input id="id_multi_field_0">
<input id="id_multi_field_1">
</div>
</div>