如何在django中运行时自动填充字段

时间:2018-02-09 15:37:39

标签: python django

我想创建一个销售订单模型表单...

model.py

class Salesorder(models.Model):
    salesorderid=models.CharField(max_length=100, primary_key=True)
    distributororder=models.ForeignKey(Distributor)
    sales_representative=models.CharField("Sales Representative",max_length=50)
    discount=models.CharField("Discount",max_length=50)
    status=models.CharField(max_length=15,choices=sale_status, default='Un Paid')
    sub_total=models.PositiveIntegerField()
    paid=models.PositiveIntegerField()
    balance=models.PositiveIntegerField()
    productsale=models.ForeignKey('product.Product')
    date = models.DateField()

view.py

def salesorder_create(request, template_name='sales/salesorder_form.html'):
    form = SalesOrderForm(request.POST or None)
    if form.is_valid():
        form.save()
        return redirect('index.html')
    return render(request, template_name, {'form':form})

模板

<div class="form-group">
        <h3>salesorderid</h3>
       {{form.salesorderid}}
        <h3>distributororder</h3>
        {{form.distributororder}}
        <h3>sales_representative</h3>
        {{form.sales_representative}}
        <h3>discount</h3>
        {{form.discount}}
        <h3>status</h3>
        {{form.status}}
        <h3>sub_total</h3>
        {{form.sub_total}}
        <h3>paid</h3>
        {{form.paid}}
        <h3>balance</h3>
        {{form.balance}}
        <h3>productsale</h3>
        {{form.productsale}}
        <h3>date</h3>
        {{form.date}}
    </div>

我希望当用户输入sub_total和付费字段时,可以使用余额字段自动填充   balance = total -paid 请帮忙怎么做

0 个答案:

没有答案