• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

How can store array of ids in foregin key field in Django

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


           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)


           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')
                          vendorCate = VendorCategory(
                          return redirect('menu')
                     except Exception as e:
                          return HttpResponse('failed{}'.format(e))


   <form method="get">
   {% csrf_token %}
           <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 }}
               {% endfor %}

       <input type="submit" value="Select">

   <form method="post">
   <input type="hidden" value={{  selectedCateogry }} name="ProductSelect">
   {% csrf_token %}
       {% for product in products%}
       <input type="checkbox" name="ProductSelect" value="{{ product.id }}" >{{ product.name }}
       {% endfor %}
   <input type="submit" value="Select">
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
    Bookmark Topic Watch Topic
  • New Topic