• 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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

 
Greenhorn
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.

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>
 
Let's go to the waterfront with this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic