Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update queryset in Django in following situation?

I want to update data in Django model like this:

 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/')

The new data is supplied by user in a form. I want to update data with id=video_id. This gives me following error:

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)
like image 842
pynovice Avatar asked Aug 05 '13 05:08

pynovice


1 Answers

The update function only take keyword arguments, no generic arguments, that's why you get the update() takes exactly 1 argument (2 given) error message.

Try:

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

Where your models are :

class Video(models.Model):
    ...

class VideoInfo(models.Model):
    foo = models.ForeignKey(Video)
    ...

Note that the doc linked in comment by lazy functor shows the signature of the update function.

like image 81
Ricola3D Avatar answered Sep 28 '22 14:09

Ricola3D