У меня есть таблица с несколькими записями. У каждого ряда есть переключатель. Я хочу назначить идентификатор строки выбранной в данный момент радиокнопке.
Я использую Django 2.1 и ModelForms, где одним из них должна быть форма RadioSelect. Однако я не совсем понимаю, как назначить значение строки переключателю.
Вот как это выглядит сейчас
Модель
class Testplan (models.Model):
testName = models.CharField(max_length=200,default='')
selectionScenario = models.CharField(max_length=200,default='')
author = models.CharField(max_length=200,default='')
type = models.CharField(max_length=200,default='', choices=testplan_type_options)
status = models.CharField(max_length=200,default='', choices=testplan_status_options)
version = models.CharField(max_length=200, default='')
created = models.CharField(max_length=200,default='')
updated = models.CharField(max_length=200,default='')
ModelForm
class TestPlanForm(forms.ModelForm):
CHOICES=[('',''),]
selectionScenario = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect()) #Since Radio is not supported in the Model.
class Meta:
model = Testplan
fields = ('testName', 'selectionScenario', 'author', 'type' , 'status', 'version', 'created', 'updated', )
widgets = {
'testName': forms.TextInput(attrs={'class' : 'form-control'}),
'author': forms.TextInput(attrs={'class' : 'form-control'}),
'type': forms.Select(attrs={'class' : 'form-control'}),
'version': forms.TextInput(attrs={'class' : 'form-control'}),
'status': forms.Select(attrs={'class' : 'form-control'}),
'created': forms.TextInput(attrs={'class' : 'form-control'}),
'updated': forms.TextInput(attrs={'class' : 'form-control'}),
}
Фрагмент HTML
<table id="dt_basic" class="table table-striped table-bordered table-hover" width="100%">
<thead>
<th>Select</th>
<th>Scenario Name</th>
<th>Scenario Functional</th>
<th>Author</th>
<th>Type</th>
<th>Version</th>
<th>Map</th>
<th>Dynamic Objects</th>
<th>Weather</th>
<th>Maneuver</th>
<th>Created</th>
<th>Updated</th>
</thead>
<tbody>
{% for item in scenario_list %}
<tr>
<!-- <td>{{ form.selectionScenario }}</td> -->
<!-- <td><input id="scenarioId" type="radio" name="rating" value={{ item.id }} class="radiobox style-2" /></td> -->
<td>{{ form.selectionScenario }}</td>
<td>{{ item.conreteScenarioName }}</td>
<td>{{ item.functionalScenario }}</td>
<td>{{ item.author }}</td>
<td>{{ item.type }}</td>
<td>{{ item.version }}</td>
<td>{{ item.map }}</td>
<td>{{ item.dynamicObject }}</td>
<td>{{ item.weather }}</td>
<td>{{ item.maneuver }}</td>
<td>{{ item.created }}</td>
<td>{{ item.updated }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Есть ли лучший способ сделать это?