如何为选定的单选按钮分配行ID?

时间:2019-05-16 14:26:23

标签: django django-models django-forms

我有一个包含多个条目的表。每行都有一个单选按钮。我要将行的ID分配给当前选择的单选按钮。

我正在使用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>

有更好的方法吗?

0 个答案:

没有答案