在这里,我将在我的产品表(db)中添加新项目。但我面临一个错误,比如
在我的产品表中我有 vendor_id 作为外键
这是我的观点.py
def add1(request):
obj = Vendor.objects.all()
return render_to_response('add_product1.html',{'obj':
obj},context_instance=RequestContext(request))
def add_product2(request):
if request.method == 'GET':
val0 = request.GET.get('options')
val1 = request.GET.get('name')
val2 = request.GET.get('price')
val3 = request.GET.get('category_product')
val4 = request.GET.get('quantity')
v = Product(Vendor= val0,name=val1,price=val2,category_product=val3,quantity= val4)
v.save()
return HttpResponseRedirect('/manage_product')
这是模型
class Vendor(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=100)
contact = models.CharField(max_length=100)
created_date = models.DateField()
class Product(models.Model):
Vendor = models.ForeignKey(Vendor)
name = models.CharField(max_length=30)
price = models.CharField(max_length=40)
category_product= models.CharField(max_length=40)
quantity = models.CharField(max_length=40)
在 add_product1.html
<form action="/add_product2/" method="POST">
id : <select name='options' >
{% for Vendor in obj %}
<option value='0' >{{Vendor.id}}</option>
{%endfor%}
</select><br />
Product Name:<input type ='text' name= 'name' value =''></br>
Price : <input type ='text' name= 'price' value = ''></br>
Category_product :<input type = 'text' name= 'category_product' value = ''></br>
Quantity: <input type ='text' name='quantity' value = ''></br>
<input type='submit' name='Submit' value='add' />
</form>