用 django 原始 sql 查询填充表单集

fill formset with django raw sql query

我使用 Django formset factory 和更新视图不​​填充子表单 原始 SQL 查询和 return 'RawQuerySet' 对象没有属性 'ordered' 错误。 对象查询设置正常但原始 SQL 查询 return 此错误。

'''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 对象查看查询集来修复。 自动完成的乘客姓名。

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 = ''