我使用 Django 表单集工厂和更新视图不使用原始 SQL 查询填充子表单并返回 'RawQuerySet' 对象没有属性 'ordered' 错误。对象查询设置良好,但原始 SQL 查询返回此错误。
'''python
formset = modelformset_factory(model=GiftVoucherSub,
form=GiftVoucherSubForm,
extra=0,
can_delete=True,
min_num=1,
validate_min=True,
)
formset = formset(request.POST or None,
queryset=queryset,
# initial=initial,
prefix='rlt_giftvoucher',
)'''
通过向 forms.py 添加额外字段并使用 django 对象查看查询集来修复。带有自动完成功能的passanger 名称。
views
queryset = GiftVoucherSub.objects.filter(main_id=id, is_deleted=False).order_by('id')
forms
class GiftVoucherSubForm(forms.ModelForm):
passanger_id = forms.CharField(max_length=30,
required=False,
widget=forms.HiddenInput()
)
passanger_name = forms.CharField(widget=forms.TextInput(attrs={
# 'id': 'form_fatura_cari_isim',
'class': 'formset-field table-condensed clearable',
'required': 'True',
'autocomplete': 'off',
'type': 'search',
'onfocus': 'fn_search_passanger(this.id)',
}
)
)
class Meta:
model = GiftVoucherSub
fields = [
'id',
'main_request_type',
'sub_request_type',
'passanger_id',
'passanger_name',
'is_deleted',
]
def __init__(self, *args, **kwargs):
super(GiftVoucherSubForm, self).__init__(*args, **kwargs)
if self.instance.passanger_id:
extra_value = BoYolcuListesi.objects.get(usertableid=self.instance.passanger_id)
self.fields['passanger_name'].initial = extra_value.isim
self.helper = FormHelper()
self.helper.form_tag = True
for field in self.fields:
self.fields[field].widget.attrs.update({'class': 'formset-field table-condensed'})
self.fields[field].label = ''
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句