I am trying to store multiple product id against the specific vendor, but when i store it show the error ' Field 'id' expected a number but got ['13', '32']. ' . I am storing the value through check box
Model.py
class VendorCategory(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
vendor = models.ForeignKey(Vendor, on_delete=models.CASCADE)
View.py
class Vendor_Category(TemplateView):
template_name = 'purchase/vendorCategory.html'
def get(self, request, *args, **kwargs):
categories = CategoryModel.objects.all()
categoryId = self.request.GET.get('SelectCategory')
products = ProductModel.objects.filter(category_id=categoryId)
args = {'categories': categories, 'products': products, 'selectedCategory': categoryId}
return render(request, self.template_name, args)
def post(self, request, vendor_id):
categoryobj = self.request.GET.get('SelectCategory')
productobj = self.request.POST.getlist('ProductSelect')
try:
vendorCate = VendorCategory(
vendor_id=VendorModel.objects.get(id=vendor_id),
category_id=categoryobj,
product_id=productobj
)
vendorCate.save()
return redirect('menu')
except Exception as e:
return HttpResponse('failed{}'.format(e))
Template
<form method="get">
{% csrf_token %}
<label>
<select name="SelectCategory" >
<option disabled="disabled" value="True" selected="{{ selectedCategory|yesno:"yeah, no, maybe" }}"> Select Category</option>
{% for category in categories %}
<option value="{{ category.id }}" selected="{% if category.id == selectedCategory %} {% endif %}">
{{ category.name }}
</option>
{% endfor %}
</select>
</label>
<input type="submit" value="Select">
</form>
<form method="post">
<input type="hidden" value={{ selectedCateogry }} name="ProductSelect">
{% csrf_token %}
<label>
{% for product in products%}
<input type="checkbox" name="ProductSelect" value="{{ product.id }}" >{{ product.name }}
{% endfor %}
</label>
<input type="submit" value="Select">
</form>