Django保存数据库中复选框选中的html表行

时间:2018-03-06 00:04:52

标签: django checkbox

我的模型中有一张表,其中存有股票,其名称为Stocks

此表在模板中显示,我想在每行旁边放一个复选框,将选中的行保存在模型的另一个表中

这里是我的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')

和模板:

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

和视图:

 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)

此错误已显示

/ sellerstocks /

的TypeError

int()参数必须是字符串,类似字节的对象或数字,而不是'list'

当我将代码更改为:

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

出现了这个错误:

/ sellerstocks /

的ValueError

无法指定“[]”:“SellerDesktop.stock”必须是“Stocks”实例。

如何将选定的行插入新表?

1 个答案:

答案 0 :(得分:1)

错误:

Cannot assign  must be a "" instance.  
当我改变时,

消失了:

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

到:

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