我有一个django-autocomplete-light定义的自动完成选择框。 我已经写了这段代码:
<script>
$(document).ready(function() {
$('#id_raste').change(function() {
var selectedValue = $(this).val();
window.alert(selectedValue);
if($(this).val() === 'abc') {
$('#id_car').closest('span').hide();
} else if (selectedValue === 'bcd') {
$('#id_car').closest('span').show();
}
});
});
</script>
所以我需要选择框ID,这些代码适用于其他类型的表单(例如Textinput等),但我不知道如何定义autocomplete.Select2吗?
class StocksForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(StocksForm, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
field.widget.attrs['style']= 'width:60%'
self.fields['raste'].widget = forms.TextInput???(attrs={
'id': 'rasteId'})
class Meta():
model=Stocks
fields=('user','car','stname','mark','description','pic','price')
widgets = {
'car': autocomplete.Select2(url='BallbearingSite:car_autocomplete'),
}
当我使用autocomplete.Select2
时,我会遇到此错误:
KeyError at /stock/
'id'