请这是一个紧急的任务我非常感谢您的建议我正在尝试制作一个在线订购系统,有人可以同时订购几个项目,为此我使用了inlineformset
并且有时使用{{form.field_name}}
来防止一些样式和js事件,为此我决定手动使用输入字段标记,在我以前的项目中我没有遇到这样的挑战,现在我困惑如何实现它!?这是我的模型。py
class Item(models.Model):
items = models.CharField(max_length=50)
#others
def __str__(self):
return self.items
class Invoice(models.Model):
seller = models.ForeignKey(User,on_delete=models.CASCADE)
customer = models.CharField(max_length=50)
items = models.ManyToManyField(Item,through='ItemsInvoice')
class ItemsInvoice(models.Model):
invoice= models.ForeignKey(Invoice,on_delete=models.CASCADE)
item = models.ForeignKey(Item,on_delete=models.CASCADE)
quantity = models.IntegerField()
price = models.IntegerField()
我在view中使用了基于函数的view,这是我的Views.py
@login_required
def createClientInoiveView(request):
item_names = Item.objects.all()
if request.method == 'POST':
customer = request.POST['customer']
seller = request.user
obj = CustomerInvoice.objects.create(seller=seller,customer=customer)
# inlineformset fields
item = request.POST['items']
quantity = request.POST['quantity']
price = request.POST['price']
cash = request.POST['cash']
discount = request.POST['discount']
items = InvoiceItem.objects.create(item=item,quantity=quantity,price=price,cash=cash,discount=discount)
with transaction.atomic():
obj = obj.save(commit=False)
if obj.is_valid and item.is_valid():
item.invoice = obj
obj.save()
item.save()
return redirect(reverse_lazy('invoiceapp:detail-invoice',kwargs={'pk':obj.pk}))
return render(request,'invoiceapp/create_invoice.html',{'item_names':item_names})
这是我的html表单
null
<form method="POST">{% csrf_token %}
{{items.management_form}}
<div class="w-full md:w-11/12 mx-auto realative p-2 bg-gray-200 wasll" style="direction: ltr !important;">
<div class="p-1 pr-2 pb-1 text-xs border border-black rounded-lg flex flex-wrap">
<div class="flex w-8/12 lg:w-9/12">
<div class="w-10/12 ml-8 border-b border-gray-600 border-dotted">
<input type="text" name="customer" class="bg-transparent w-full text-right focus:outline-none" id="">
</div>
<div class="">
: name
</div>
</div>
</div>
<!-- table -->
<div class="mt-1 border border-black">
<!-- header -->
<div class="flex flex-wrap grayBG text-sm text-white">
<div class="w-16 md:w-1/12 border-r text-center">
price
</div>
<div class="w-16 md:w-2/12 border-r text-center">
quantity
</div>
<div class="w-20 md:w-2/12 border-r text-center">
items
</div>
</div>
<!-- inputs -->
<div id="allInp">
<div class="flex flex-wrap grayBG text-sm text-black inp">
<div class="w-16 md:w-1/12 p-2 border-r text-center ">
<input type="number" name="price" onkeyup="totalSum()" class="rounded-lg nrx focus:outline-none py-1 w-full">
</div>
<div class="w-16 md:w-2/12 p-2 border-r text-center ">
<input type="number" name="quantity" onkeyup="totalSum()" class="rounded-lg focus:outline-none py-1 w-full">
</div>
<div class="w-20 md:w-2/12 p-2 border-r text-center">
<datalist id="types">
{% for i in item_names %}
<option value="{{i}}">
{% endfor %}
</datalist>
<input type="text" name="items" list="types" class="w-full rounded-lg focus:outline-none py-1">
</div>
</div>
</div>
</div>
</div>
<div class="w-6/12 text-center mt-1 mx-auto mb-6">
<button class="w-full bg-white text-gray-900" id="submit">submit</button>
</div>
</form>
<script>
$(function(){
$('#allInp').formset({
prefix:'{{items.prefix}}',
addText:'add new row',
addCssClass:'btn btn-info ',
deleteText:'delete',
deleteCssClass:'delFun',
})
</script>
null
但我一直收到这个错误信息
无法分配“'mouse'”:“ItemsInvoice.Item”必须是“Item”实例。
还有这些我的表格
class CustomerInvoiceForm(forms.ModelForm):
class Meta:
model = Invoice
fields = [
'customer'
]
class CustomerInvoiceItemForm(forms.ModelForm):
item = forms.ModelChoiceField(queryset=Item.objects.all(),empty_label='---')
class Meta:
model = ItemsInvoice
fields = ['item','quantity','price']
CustomerInvoiceInlineFormset = inlineformset_factory(
Invoice,ItemsInvoice,form=CustomerInvoiceItemForm,
fields=('item','quantity','price'),extra=1
)
我也试着用这个
#other code
obj=CustomerInvoiceItemForm()
#instead of > CustomerInvoice.objects.create(seller=seller,customer=customer)
#other code
#and using this > items = CustomerInvoiceInlineFormset(request.POSt)
#instead of > items = InvoiceItem.objects.create(item=item,quantity=quantity,price=price,cash=cash,discount=discount)
却什么也没省!?难道我不应该改变一些东西来得到我所想要的东西吗?!
在template中,输出
“tem”。__str__
作为关键字,因此不能解析任何“tem
。
更改为