' Pedido_Extra'对象没有属性'更新'

时间:2017-03-30 16:26:38

标签: python django

我在更新已保存对象的字段时遇到问题,首先使用ped.id捕获id,然后将其传递给get(id = ped.id)然后执行.update,这就是错误,其中经过几种方式测试是我需要你的帮助的原因,请提前谢谢!

def IngresarExtra(request, id_especialidad, cod_experto):
   especialidad = Especialidad.objects.get(id=id_especialidad)
   articulo = Articulo.objects.get(pk=cod_experto)
   if request.method == 'GET':
      form = ExtraForm()
   else:
      form = ExtraForm(request.POST)
         if form.is_valid():
             ped = form.save()
             ped.id
             ped_ex = Pedido_Extra.objects.get(id=ped.id).update(articulo_ex=articulo)
      ped_ex.save()
  return HttpResponseRedirect('/solicitar/pedidos-extra/')

return render(request,' form2.html',{' form':form,' especialidad':especialidad,' articulo' :articulo})

class Pedido_Extra(models.Model):
    articulo_ex       = models.ForeignKey('Articulo')
    especialidad_ex   = models.ForeignKey('Especialidad')
    fecha_pedido_ex   = models.DateTimeField(auto_now_add=True,null=True, blank=True)
    fecha_entrega_ex  = models.DateTimeField(auto_now_add=False)
    cantidad_ex       = models.IntegerField(blank=True, default=0)
    estado_ex         =  models.CharField(max_length=20, blank=True, default='pendiente')

    def __str__(self):
        return '{}'.format(self.articulo_ex, self.especialidad_ex, self.estado_ex, self.cantidad_ex) 

1 个答案:

答案 0 :(得分:3)

来自docs

  

有时您希望将字段设置为QuerySet中所有对象的特定值。您可以使用update()方法执行此操作。

您无法将update()get()一起使用。这是一次批量操作。

您可以尝试以下方法:

相关问题