Update database - Making queries

Hello everyone,

When I click form button, I add new sample to db. And also, I want to update another specific fields of another rows in db at the same time. There are a few rows which have same p_id.

What I want to do is to make YES is_index_sample of the rows matching hiddenIndexID. Make those that don’t match NO. In the last step, assigning NO to is_index_sample of the new sample value I created. But secondItem.is_index_sample part is not working. How can I fix it? Thank you in advance.


sample = Samples()
cycle = Samples.objects.filter(patientid = request.POST.get('p_id'));
check = request.POST.get('hiddenIndexID');
if check is not None:
   for secondItem in cycle:
      if str(secondItem.id) == check:
         secondItem.is_index_sample = 'Yes';
         secondItem.is_index_sample = 'No';
   sample.is_index_sample = 'No';


messages.success(request, "New sample is added successfully!")

return HttpResponseRedirect("/")

I think you’re missing a secondItem.save() at the end of your loop

Yes, it is possible. Any changes you do to a model instance won’t get persisted if you don’t call its .save() method.

1 Like

Hello, I fixed the problem with