不能分配“'str值'”:“详细table.voucher_id”必须是“主表”实例

问题描述:

我想创建一个窗体使用我的主表和细节表。 主表:TmpPlInvoic(主键:voucher_id) 详细信息表:TmpPlInvoicedet(外键:voucher_id,对一个凭证ID多个条目)不能分配“'str值'”:“详细table.voucher_id”必须是“主表”实例

错误 无法分配 “ '5453661'”:“TmpPlInvoicedet.voucher_id “必须是”TmpPlInvoice“实例。

我不是为什么我得到这个实例的错误。问题中的凭证标识由我创建并插入主表中。然后我尝试通过从主表获取它到优惠券ID,以便它作为一个实例来传递。

tmpPlInvoicedet_save = TmpPlInvoicedet.objects.create(
      voucher_id=voucher_id, 

Views.py

def new_invoice(request): 
    logger.info('FORM INFO ----------------------------------------------') 
    QuestionFormSet = formset_factory(AddRowsForm, extra=5) 
    QuestionFormSet_one = formset_factory(AddRowsForm, extra=1) 

if request.method == 'POST': 
    form = NameForm(request.POST) 
    formset = QuestionFormSet(request.POST) 
    temp_vid = TmpPlInvoice.objects.order_by().values_list("voucher_id", flat=True).distinct() 
    items = Item.objects.all().values_list('stock_code', 'description') 
    if form.is_valid(): 
     data = form.cleaned_data 
     logger.info('FORM INFO') 
     new_date = data['new_date'] 
     new_parc = data['new_parc'] 
     new_vid = data['new_vid'] 

     item_combo = data['item_combo'] 
     pos_combo = data['pos_combo'] 
     cus_combo = data['cus_combo'] 
     if not temp_vid: 
      voucher_id = 0 
      voucher_id += 1 
     else: 
      voucher_id = str(int(max(temp_vid)) + 1).zfill(4) 
     tmpPlInvoice_save = TmpPlInvoice.objects.create(voucher_date=new_date, 
                 voucher_id=new_vid, 
                 acct_id=item_combo, 
                 particulars=new_parc, 
                 pos_code=pos_combo, 
                 cust_id=cus_combo 
     tmpPlInvoice_save.save() 
     for detail in enumerate(formset.cleaned_data, 1): 
      tmpPlInvoicedet_save = TmpPlInvoicedet.objects.create(
       voucher_id=voucher_id, 
       lineitem=detail[0], 
       particulars=detail[1]['Rate'], 
       qty=detail[1]['Quantity'], 
       rate=detail[1]['Rate'], 
       itemtot=int(detail[1]['Rate']) * int(detail[1]['Quantity']), 
       stock_code=detail[1]['StockCode'] 
      ) 
      tmpPlInvoicedet_save.save() 

    else: 
     message = "wrong" 
     logger.error('Form is invalid. Errors are %s', form.errors) 
    return render_to_response('new_invoice.html', 
           {'formset': QuestionFormSet(), 'form': form, 'formset_single': QuestionFormSet_one}, 
           context_instance=RequestContext(request)) 
else: 
    logger.info('Method is GET') 
    form = NameForm() 
    formset = QuestionFormSet() 
    # logger.info('Formset from GET is %s', formset) 
    return render_to_response('new_invoice.html', 
           {'formset': QuestionFormSet(), 'form': form, 'formset_single': QuestionFormSet_one}, 
           context_instance=RequestContext(request)) 

从forms.py 万事达形式获取数据

class NameForm(forms.Form): 
combo_final = ([('', 'Select One')]) 
combo_final1 = ([('', 'Select One')]) 
combo_final2 = ([('', 'Select One')]) 
combo_final3 = ([('', 'Select One')]) 

items = list(Item.objects.all().values_list('stock_code', 'description')) 
item_final = combo_final.extend(items) 

pos = Positions.objects.all().values_list('pos_code', 'description') 
pos_final = combo_final1.extend(pos) 

cus = Custodian.objects.all().values_list('acct_id', 'description') 
cus_final = combo_final2.extend(cus) 

temp_vid = TmpPlInvoice.objects.order_by().values_list("voucher_id", flat=True).distinct() 
if not temp_vid: 
    voucher_id = 0 
    voucher_id += 1 
else: 
    voucher_id = str(int(max(temp_vid)) + 1).zfill(4) 

pos_combo = forms.CharField(widget=forms.Select(choices=combo_final1, attrs={ 'class': 'select2_single form-control', 'id': 'new_loc', 'blank': 'True'})) 

cus_combo = forms.CharField(widget=forms.Select(choices=combo_final2, attrs={'class': 'select2_single form-control', 'id': 'new_cus', 'blank': 'True'})) 

item_combo = forms.CharField(widget=forms.Select(choices=combo_final, attrs={'class': 'select2_single form-control', 'id': 'new_item', 'blank': 'True'})) 

new_parc = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Add Particulars', 'required':'False'})) 
new_vid = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '', 'required':'False', 'name': 'voucher_id', 'readonly': 'readonly', 'value': voucher_id})) 
new_date = forms.DateField(widget=forms.TextInput(attrs={'tabindex': '-1', 'class': 'form-control has-feedback-left', 'id': 'single_cal1','aria-describedby': 'inputSuccess2Status'})) 

详细表单集

class AddRowsForm(forms.Form): 
combo_final4 = ([('', 'Select One')]) 
items = list(Item.objects.all().values_list('stock_code', 'description')) 
item_final = combo_final4.extend(items) 

Serial = forms.BooleanField(widget=forms.CheckboxInput(attrs={'tabindex': '-1', 'class': 'flat', 'required':'False'})) 
# table_index = forms.CharField(widget=forms.HiddenInput(attrs={'required': 'False'})) 
Particulars = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control ', 'placeholder': 'Add Particulars', 'required':'False'})) 
Quantity = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Add Quantity', 'required':'False'})) 
Rate = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Add Rate', 'required':'False'})) 
Amount = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Add Amount', 'required':'False'})) 
StockCode = forms.IntegerField(widget=forms.Select(choices=combo_final4, attrs={'class': 'select2_single form-control', 'id': 'new_item', 'blank': 'True'})) 
+0

请家伙,我真的需要帮助。 – whatIf

+0

因为孩子需要成为一个实例,而不仅仅是一个ID。你有没有经历过这个教程?这样做比你想要做的要容易得多。 –

+0

哪位教程@MichaelJosephson?你可以分享网址吗? – whatIf

在这里回答我自己的问题。

我后来转移到具有内置保存功能的模型窗体,以便摆脱所有这些麻烦。如果有人需要使用此方法,请确保您创建主表的实例并将其传递到详细表中。

for detail in enumerate(formset.cleaned_data, 1): 
     tmpPlInvoicedet_save = TmpPlInvoicedet.objects.create(
      voucher_id=voucher_id, 
      lineitem=detail[0], 
      particulars=detail[1]['Rate'], 
      qty=detail[1]['Quantity'], 
      rate=detail[1]['Rate'], 
      itemtot=int(detail[1]['Rate']) * int(detail[1]['Quantity']), 
      stock_code=detail[1]['StockCode'] 
     ) 
     tmpPlInvoicedet_save.save() 

for detail in enumerate(formset.cleaned_data, 1): 
     tmpPlInvoicedet_save = TmpPlInvoicedet.objects.create(
      voucher_id=TmpPlInvoice.objects.filter(voucher_id=voucher_id), 
      lineitem=detail[0], 
      particulars=detail[1]['Rate'], 
      qty=detail[1]['Quantity'], 
      rate=detail[1]['Rate'], 
      itemtot=int(detail[1]['Rate']) * int(detail[1]['Quantity']), 
      stock_code=detail[1]['StockCode'] 
     ) 
     tmpPlInvoicedet_save.save()