1

I have a table in my models which the stocks are saving in it and its name is Stocks

this table is desplayed in a template and i want to put a checkbox beside each row to save the checked row in another table of the model

here ismy model.py :

class Stocks(models.Model):
   user=models.ForeignKey(User, null=True)
   name=models.CharField(max_length=128,verbose_name=_('stockname'))
   number=models.CharField(blank=True,null=True,max_length=64,verbose_name=_('number'))

   brand=models.CharField(max_length=64, validators=[
    RegexValidator(regex='^[A-Z]*$',message=_(u'brand must be in Capital letter'),)]
    ,verbose_name=_('brand'))
    comment=models.CharField(blank=True,null=True,max_length=264,verbose_name=_('comment'))
    price=models.PositiveIntegerField(blank=True,null=True,verbose_name=_('price'))
    date=models.DateTimeField(auto_now_add = True,verbose_name=_('date'))
    confirm=models.CharField(choices=checking,max_length=12,verbose_name=_('confirmation'), default=_('pending'))
    def __str__(self):
      return str(self.id)
    class Meta:
      verbose_name=_('Stock')
      verbose_name_plural=_('Stocks')
    def get_absolute_url(self):
      return reverse('BallbearingSite:mystocks' )


class SellerDesktop(models.Model):
  seller=models.OneToOneField(User, related_name='seller', blank=True, null=True)
  buyer=models.OneToOneField(User, related_name='buyer', blank=True, null=True)
  stock=models.ForeignKey(Stocks, related_name='stocktoseller', blank=True, null=True)
  def __str__(self):
     return str(self.seller) + '-' + str(self.buyer)
  class Meta:
     verbose_name=_('SellerDesktop')
     verbose_name_plural=_('SellerDesktop')

and the Template :

<form method="post">
 {% csrf_token %}
<table  id="example" class="table table-list-search table-responsive table-hover table-striped" width="100%">


         {% for item in myst %}

             <td><input type="checkbox" name="sendtoseller" value="{{ item.id }}"></td>
             <td>{{ item.user.profile.companyname}}</td>
             <td>{{ item.name }}</td>
             <td>{{ item.brand }}</td>
             <td>{{ item.number }}</td>
             <td>{{ item.pasvand }}</td>
             <td>{{ item.comment }}</td>
             <td>{{ item.price }}</td>
             <td>{{ item.date|timesince }}</td>

         </tr>

         {% endfor %}


  </table>

       <div style="text-align: center;  margin-top:0.5cm; margin-bottom:1cm;">
         <input type="submit" name="toseller" value="Submit to seller "  style="color:red; width:100%;"/>
       </div>
      </form>

and the view :

 def allstocks_view(request):
   if request.method=='POST':
      tosave = request.POST.getlist('sendtoseller')
      stockid=Stocks.objects.filter(id=tosave) 
      SellerDesktop.objects.create(buyer=request.user,stock=stockid)

   stocks_list=Stocks.objects.all().filter(confirm=_('approved') ).order_by('-date')

      #paginating for table
   page = request.GET.get('page', 1)
   paginator = Paginator(stocks_list, 15)
   try:
     myst = paginator.page(page)
   except PageNotAnInteger:
     myst = paginator.page(1)
   except EmptyPage:
     myst = paginator.page(paginator.num_pages)

   context={
        'allstocks':stocks_list,
        'myst':myst,
    }
   return render(request,'BallbearingSite/sellerstocks.html',context)

this error was showed up

TypeError at /sellerstocks/

int() argument must be a string, a bytes-like object or a number, not 'list'

when i changed the code to :

         stockid=Stocks.objects.filter(id=tosave[0])

this error was showed up:

ValueError at /sellerstocks/

Cannot assign "[]": "SellerDesktop.stock" must be a "Stocks" instance.

How can i insert the selected rows into new table?

3
  • you are getting a list in tosave, so for stockid you should use Stocks.objects.filter(id__in=tosave) Commented Mar 6, 2018 at 1:03
  • it also has this error :ValueError at /sellerstocks/ Cannot assign "[<Stocks: 147>]": "SellerDesktop.stock" must be a "Stocks" instance. Commented Mar 6, 2018 at 7:54
  • use indexing to get the value. [<Stocks: 147>] is a queryset so you need to access its values as you access the values from list. Commented Mar 6, 2018 at 8:15

1 Answer 1

1

the error :

Cannot assign  must be a "" instance.  

was gone when i changed :

Stocks.objects.filter(id=tosave[i])

to :

Stocks.objects.get(id=tosave[i])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.