【问题标题】:fill formset with django raw sql query用 django 原始 sql 查询填充表单集
【发布时间】:2019-09-10 09:17:40
【问题描述】:

我使用 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',
                  )'''

【问题讨论】:

  • 你的代码在哪里?
  • 我添加了查询集来自原始 sql 查询的代码。

标签: django formset


【解决方案1】:

通过向 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 = ''

【讨论】:

    猜你喜欢
    • 2015-11-21
    • 2012-03-16
    • 2014-10-08
    • 2018-05-15
    • 2016-12-21
    • 1970-01-01
    • 2013-04-17
    • 2019-08-05
    • 2017-04-08
    相关资源
    最近更新 更多