Reputation: 716
I have a function that should update all objects that are created by a user to an inactive state. So i coded this:
def TurnOff(request, passed_id):
if request.user.is_authenticated():
#this should set all objects to an inactive state
tmp = myModel.objects.filter(created_by=request.user).update(active=False)
#this is to set one specific object to an active state again
myModel = get_object_or_404(myModel, created_by=request.user, pk=passed_id)
tmp = myModel.objects.filter(created_by=request.user, pk=passed_id).update(active=True)
return HttpResponseRedirect("../started")
else:
return HttpResponseRedirect("/")
The line tmp = myModel.objects.filter(created_by=request.user).update(active=False)
is not working (that means false is not set), only if i specify the filter with the additional keyword pk=ANY_ID
, but i want to update all objects like its described in the docs!
Upvotes: 0
Views: 548
Reputation: 404
In this case, replace your following line,
tmp = myModel.objects.filter(created_by=request.user, pk=passed_id).update(active=True)
with the following :
tmp.active = False
tmp.save()
EDITED
Since 'tmp' is an object of your myModel and it returns only one object since you're using pk for retrieving it.
Upvotes: 4
Reputation: 2302
Change
# after this line myModel is not Model anymore, but instance of myModel
# with created_by=request.user and pk=passed_id
myModel = get_object_or_404(myModel, created_by=request.user, pk=passed_id)
# so this line doesn't work
tmp = myModel.objects.filter(created_by=request.user, pk=passed_id).update(active=True)
to
# you want to update _one_ instance, don't you?
tmp = get_object_or_404(myModel, created_by=request.user, pk=passed_id)
tmp.active = True
tmp.save()
Upvotes: 0