在以下情况下更新Django中的查询集?

pyn*_*ice 3 django django-queryset sql-update

我想更新Django模型中的数据,如下所示:

 video_id = request.POST['video_id']
     # Get the form data and update the data

 video = VideoInfoForm(request.POST)

 VideoInfo.objects.filter(id=video_id).update(video)

  return HttpResponseRedirect('/main/')
Run Code Online (Sandbox Code Playgroud)

新数据由用户以表格形式提供.我想用更新数据id=video_id.这给了我以下错误:

update() takes exactly 1 argument (2 given)
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in dispatch
  86.         return handler(request, *args, **kwargs)
File "/home/zurelsoft/virtualenv/videoManagement/VideoManagementSystem/video/views.py" in post
  126.          VideoInfo.objects.filter(id=video_id).update(video)

Exception Type: TypeError at /updateVideo/
Exception Value: update() takes exactly 1 argument (2 given)
Run Code Online (Sandbox Code Playgroud)

小智 11

update函数只接受关键字参数,没有泛型参数,这就是您收到update() takes exactly 1 argument (2 given)错误消息的原因.

尝试:

VideoInfo.objects.filter(id=video_id).update(foo=video)
Run Code Online (Sandbox Code Playgroud)

您的模型在哪里:

class Video(models.Model):
    ...

class VideoInfo(models.Model):
    foo = models.ForeignKey(Video)
    ...
Run Code Online (Sandbox Code Playgroud)

请注意,lazy functor在注释中链接的doc显示了该update函数的签名.