我有一个“产品”模型,其中提供了产品的所有详细信息,而我有一个“订单”模型,其中带有产品模型的外键。我正在过滤特定字段上的详细信息,并尝试在Order Model中添加该字段。 我将这种概念应用于特定领域,例如:Product.objects.filter(pname ='Nokia1200')。values('pname','catgry','brand')。 但这产生了一条错误消息。
<form class="well form-horizontal" method="post" action="{% url 'new_order' %}">
{% csrf_token %}
<fieldset>
{% for n in ListPrdt %}
<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">
<option>{{n.pname}}</option>
</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>
<input id="fullName" name="catgry" placeholder="Full Name" class="form-control" required="true" value="{{n.catgry}}" type="text">
</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>
<input id="fullName" name="brand" placeholder="Full Name" class="form-control" required="true" value="{{n.brand}}" type="text">
</div>
</div>
</div>
{% endfor %}
<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>
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
def NewOrder(request):
add_prdt = Product.objects.filter(pname='Nokia1200').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
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')
答案 0 :(得分:0)
您的过滤器查询是:
product=Product.objects.filter(name='Nokia1200').first()
Order.objects.create(pname=product,.......)
答案 1 :(得分:0)
似乎“订单”表中的pname是产品表的外键。您不能将值分配给外键列,而应该分配产品对象。
做这样的事情。
pn=Product.objects.get(pname='nokia')
Order.objects.create(pname=pn,.....)