在我的django项目中,我的代码如下:
forms.py
class addOrderForm(ModelForm):
class Meta:
model = Orders
fields = [ 'shipvia', 'customer', 'employee', 'orderdate', 'freight', 'shipname', 'shipaddress', 'shipcity', 'shipregion', 'shippostalcode', 'shipcountry' ]
views.py
def addOrder(request):
OrderDetailsFormSet = inlineformset_factory(Orders,
OrderDetails,
fields=('product', 'unitprice', 'quantity' , 'discount'),
extra=3)
order=Orders()
if request.method == 'POST':
f = addOrderForm(request.POST, instance=order)
fs = OrderDetailsFormSet(request.POST,instance=order)
if fs.is_valid() and f.is_valid():
f.save()
fs.save()
return HttpResponse('success')
else:
f = addOrderForm(instance=order)
fs = OrderDetailsFormSet(instance=order)
return render(request, 'orders/addOrder.html', context = {'fs': fs,'f':f,'order':order})
orders/addOrder.html
{% block body %}
<form action="/orders/addOrder/" method="post">
{% csrf_token %}
<div class="row">
{{ fs.management_form }}
<table>
{% for form in fs.forms %}
{% for field in form %}
<tr><th>{{field.label_tag}}</th><td>{{field}}{{field.errors}}</td></tr>
{% endfor %}
{% endfor %}
</table>
</div>
<input type="submit" value="Submit" />
</form>
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker({
inline: true,
sideBySide: true,
format: 'YYYY-MM-DD',
});
});
</script>
{% endblock %}
数据库中有两个表
ORDERS表:
orderid | smallint | not null default nextval('orders_orderid_seq'::regclass)
customerid | bpchar |
employeeid | smallint |
orderdate | date |
requireddate | date |
shippeddate | date |
shipvia | smallint |
freight | real |
shipname | character varying(40) |
shipaddress | character varying(60) |
shipcity | character varying(15) |
shipregion | character varying(15) |
shippostalcode | character varying(10) |
shipcountry | character varying(15) |
Indexes:
"pk_orders" PRIMARY KEY, btree (orderid)
Foreign-key constraints:
"fk_orders_customers" FOREIGN KEY (customerid) REFERENCES customers(customerid)
"fk_orders_employees" FOREIGN KEY (employeeid) REFERENCES employees(employeeid)
"fk_orders_shippers" FOREIGN KEY (shipvia) REFERENCES shippers(shipperid)
Referenced by:
TABLE "order_details" CONSTRAINT "fk_order_details_orders" FOREIGN KEY (orderid) REFERENCES orders(orderid)
ORDER_DETAILS表:
orderid | smallint | not null
productid | smallint | not null
unitprice | real | not null
quantity | smallint | not null
discount | real | not null
orderdetailid | integer | not null default nextval('orderdetails_orderdetailid_seq'::regclass)
Indexes:
"pk_order_details" PRIMARY KEY, btree (orderdetailid)
"uk_order_details" UNIQUE CONSTRAINT, btree (orderid, productid)
Foreign-key constraints:
"fk_order_details_orders" FOREIGN KEY (orderid) REFERENCES orders(orderid)
"fk_order_details_products" FOREIGN KEY (productid) REFERENCES products(productid)
在具有上述django代码的当前状态下,我能够添加新的Orders记录(但仅填充orderid列,行中的所有其他列都为NULL)和完整的Order_details行(从表单中获取)。 我的问题是如何为Orders表提供剩余列数据,而不仅仅是auto orderid?
答案 0 :(得分:0)
最后我找到了它,只需在模板中调用f,如下所示,将出现Orders表单字段,并将在数据库提交期间添加:
订单/ addOrder.html
{% block body %}
<form action="/orders/addOrder/" method="post">
{% csrf_token %}
<div class="row">
{{ f }} <!- new line is HERE -->
{{ fs.management_form }}
<table>