我正在尝试使用我的主表和详细信息表创建表单。 主表:TmpPlInvoic(主键:voucher_id) 详细信息表:TmpPlInvoicedet(外键:voucher_id,针对一个凭证ID的多个条目)
错误 无法分配"' 5453661'":" TmpPlInvoicedet.voucher_id"必须是" TmpPlInvoice"实例
我不是为什么我得到这个实例错误。问题中的凭证ID由我创建并插入主表中。然后我尝试通过从主表到详细信息表获取凭证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 :(得分:0)
在这里回答我自己的问题。
我后来转移到具有内置保存功能的模型表单,因此摆脱了所有这些麻烦。如果任何人需要使用此方法,请确保您创建主表的实例并将其传递给详细信息表。
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()