1

I have the following code -

 testcase = Table1.objects.filter(ID=id,serialno ='1234', condition='False').exists()
 if not testcase:
     c = Table1.objects.filter(ID=id,serialno ='1234').select_for_update().update(condition='True')

Here, I am querying the same DB twice. Is there any way where I can do the filter exists and update in a single query?

2 Answers 2

1

Filtering doesn't send a query. I agree with the answer above, but if you want a correction for you code, then this would be correct:

testcase = Table1.objects.filter(ID=id,serialno ='1234')
if not testcase.filter(condition='False'):
     c = testcase.select_for_update().update(condition='True')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the correction. I will update my code. Also, will something like this work? testcase = Table1.objects.filter(Id=id, serialno='1234').select_for_update().update(condition=When(~Q(condition='False'), then=Value('True')))
0

You can just use the update method with your filter query.

Table1.objects.filter(ID=id,serialno ='1234', condition='False').update(condition=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.