usman imtiaz

Greenhorn
+ Follow
since Mar 27, 2021
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by usman imtiaz

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 don't know how can i fix this issue.

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>