我有一个 'Product' 模型,其中产品的所有细节都可用,我有 'Order' 模型,外键为 Product 模型 . 我想在 From.html 中有一个下拉列表,其中包含Product model的所有产品名称,如果我选择其中任何一个,那么所有细节都应自动填入输入字段 . 我在这样的特定领域应用了这个概念:

Product.objects.filter(pname='Lenovo K8 Plus').values('pname','catgry','brand')

From.Html

<form class="well form-horizontal" method="post" action="{% url 'new_order' %}">
                {% csrf_token %}
                  <fieldset>

                     <div class="form-group">
                        <label class="col-md-4 control-label">Select Product</label>
                        <div class="col-md-6 inputGroupContainer">
                           <div class="input-group">
                              <span class="input-group-addon" style="max-width: 100%;"><i class="glyphicon glyphicon-list"></i></span>
                              <select class="selectpicker form-control" name="pname">
                                 {% for n in ListPrdt %}
                                 <option>{{n.pname}}</option>
                                 {% endfor %}
                              </select>
                           </div>
                        </div>
                     </div>

                    <div class="form-group">
                        <label class="col-md-4 control-label">Category Name</label>
                        <div class="col-md-6 inputGroupContainer">
                           <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                            {% for n in ListPrdt %}
                            <input id="fullName" name="catgry" placeholder="Full Name" class="form-control" required="true" value="{{n.catgry}}" type="text">
                            {% endfor %}

                          </div>
                        </div>
                     </div>

                     <div class="form-group">
                        <label class="col-md-4 control-label">Brand Name</label>
                        <div class="col-md-6 inputGroupContainer">
                           <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                            {% for n in ListPrdt %}
                            <input id="fullName" name="brand" placeholder="Full Name" class="form-control" required="true" value="{{n.brand}}" type="text">
                            {% endfor %}

                          </div>
                        </div>
                     </div>

                       <div class="form-group">
                        <label class="col-md-4 control-label">Quantity</label>
                        <div class="col-md-6 inputGroupContainer">
                           <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span><input id="email" name="qnty" placeholder="Email" class="form-control" required="true" value="" type="text"></div>
                        </div>
                     </div>
                     <div class="form-group">
                        <label class="col-md-4 control-label">Price</label>
                        <div class="col-md-6 inputGroupContainer">
                           <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span><input id="phoneNumber" name="price" placeholder="Phone Number" class="form-control" required="true" value="" type="text"></div>
                        </div>
                     </div>

                     <div class="form-group">
                        <label class="col-md-4 control-label">Tax</label>
                        <div class="col-md-6 inputGroupContainer">
                           <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span><input id="addressLine2" name="tax" placeholder="Address Line 2" class="form-control" required="true" value="" type="text"></div>
                        </div>
                     </div>
                     <div class="form-group">
                        <label class="col-md-4 control-label">Total</label>
                        <div class="col-md-6 inputGroupContainer">
                           <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span><input id="city" name="total" placeholder="City" class="form-control" required="true" value="" type="text"></div>
                        </div>
                     </div>
                     <div class="form-group">
                        <label class="col-md-4 control-label">Delivery Date</label>
                        <div class="col-md-6 inputGroupContainer">
                           <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span><input id="state" name="deliverydt" placeholder="State/Province/Region" class="form-control" required="true" value="" type="text"></div>
                        </div>
                     </div>
                     <div class="form-group">
                        <label class="col-md-4 control-label">Remarks</label>
                        <div class="col-md-6 inputGroupContainer">
                           <div class="input-group"><span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span><input id="postcode" name="remark" placeholder="Postal Code/ZIP" class="form-control" required="true" value="" type="text"></div>
                        </div>
                     </div>
                     <div class="form-group">
                        <label class="col-md-4 control-label">Select Brand</label>
                        <div class="col-md-6 inputGroupContainer">
                           <div class="input-group">
                              <span class="input-group-addon" style="max-width: 100%;"><i class="glyphicon glyphicon-list"></i></span>
                              <select class="selectpicker form-control" name="conrej" disabled>

                                 <option>Confirm</option>
                                 <option>Rejected</option>

                              </select>
                           </div>
                        </div>
                     </div>
                    <button>Submit</button>

                  </fieldset>
               </form>

Models.Py

class Order(models.Model):
    Reject = 'RJ'
    Confirm = 'CN'
    confchoice = ((Reject, 'Reject'),(Confirm, 'Confirm'),)
    pname = models.ForeignKey(Product, on_delete=models.CASCADE)
    catgry = models.CharField(max_length=10)
    brand = models.CharField(max_length=50)
    qnty = models.CharField(max_length=10)
    price = models.CharField(max_length=20)
    tax = models.CharField(max_length=20)
    total = models.CharField(max_length=20)
    deliverydt = models.CharField(max_length=20)
    remark = models.CharField(max_length=10)
    conrej = models.CharField(max_length=10, choices=confchoice, default="Reject")

    def __str__(self):
        return self.remark

Views.py文件

def NewOrder(request):
    add_prdt = Product.objects.filter(pname='Lenovo K8 Plus').values('pname','catgry','brand')
    return render(request, 'neworder.html', {'ListPrdt' : add_prdt})

产品型号

class Product(models.Model):
   pname = models.CharField(max_length=50)
   catgry = models.CharField(max_length=20)
   brand = models.CharField(max_length=20)
   price = models.CharField(max_length=10)
   qnty = models.CharField(max_length=10)
   vname = models.CharField(max_length=50)
   mfd = models.CharField(max_length=20)
   exp = models.CharField(max_length=10)
   height = models.CharField(max_length=10)
   width = models.CharField(max_length=10)
   color = models.CharField(max_length=10)
   wght = models.CharField(max_length=10)
   remark = models.CharField(max_length=50)
   hsn = models.CharField(max_length=10)

   def __str__(self):
       return self.pname

Views.py文件(用于在订单模型中添加新订单)

def new_order(request):
   pname = request.POST.get("pname", False)
   catgry = request.POST.get("catgry", False)
   brand = request.POST.get("brand", False)
   qnty = request.POST.get("qnty", False)
   price = request.POST.get("price", False)
   tax = request.POST.get("tax", False)
   total = request.POST.get("total", False)
   deliverydt = request.POST.get("deliverydt", False)
   remark = request.POST.get("remark", False)
   OrderNew = Order(pname = pname, catgry = catgry, brand = brand, qnty =  qnty, price = price, tax = tax, 
                total = total, deliverydt = deliverydt, remark = remark)
OrderNew.save()

   return render(request,'neworder.html')