如果此表单是嵌入式表单集的一部分,如何使用Django __init__方法访问提交的数据(字段值)?

时间:2019-06-30 18:39:13

标签: django django-forms inline-formset

我有表格

class OrderItemsImportForm( forms.ModelForm ):
  oem = forms.CharField(max_length=128, required=True)
  field_order = ['oem', ....]

  class Meta:
    model = Orderitem
    fields = (.....,)

  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

    if self.is_bound:
        oem = self.data.get('oem')
    else:
        oem = kwargs['initial']['oem']

    self.fields['part'].queryset = Part.objects.filter(oem=oem)

但是代码

oem = self.data.get('oem')

不返回任何值,因为如果我打印self.data,则其中包含以下内容:

data = <QueryDict: {'csrfmiddlewaretoken': ['HOCNE7mZmuIXz0RvabrEBFlGQU1qwvThtdj2xtMw8n6tsYGbhq2jNze1JFHSVcIL'], 'ordercomposingitem_set-TOTAL_FORMS': ['2'], 'ordercomposingitem_set-INITIAL_FORMS': ['0'], 'ordercomposingitem_set-MIN_NUM_FORMS': ['0'], 'ordercomposingitem_set-MAX_NUM_FORMS': ['1000'], 'ordercomposingitem_set-0-id': [''], 'ordercomposingitem_set-0-order': [''], 'ordercomposingitem_set-0-oem': ['8844026100'], 'ordercomposingitem_set-0-count': ['100'], 'ordercomposingitem_set-0-price_prefered': ['0'], 'ordercomposingitem_set-0-part': ['88'], 'ordercomposingitem_set-0-user_reference': ['12345'], 'ordercomposingitem_set-0-user_partnumber': ['MY-12345'], 'ordercomposingitem_set-0-user_partbrand': ['My-Supa-Brand'], 'ordercomposingitem_set-0-user_partname': ['My Partname'], 'ordercomposingitem_set-0-user_price': ['100'], 'ordercomposingitem_set-0-user_comment': ['Please make it better!'], 'ordercomposingitem_set-1-id': [''], 'ordercomposingitem_set-1-order': [''], 'ordercomposingitem_set-1-oem': ['31170RNAA02'], 'ordercomposingitem_set-1-count': ['150'], 'ordercomposingitem_set-1-price_prefered': ['0'], 'ordercomposingitem_set-1-part': [''], 'ordercomposingitem_set-1-user_reference': ['789456'], 'ordercomposingitem_set-1-user_partnumber': [''], 'ordercomposingitem_set-1-user_partbrand': ['My-Supa-Brand'], 'ordercomposingitem_set-1-user_partname': [''], 'ordercomposingitem_set-1-user_price': [''], 'ordercomposingitem_set-1-user_comment': ['Hello!'], 'action': ['save']}>

没有干净的“ oem”字段,只有“ ordercomposeditem_set-0-oem”之类。

如何访问oem字段值?

0 个答案:

没有答案