How can store list of id in foreign key django

0 votes
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>
Mar 27, 2021 in Python by usman

edited Mar 4 60 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP