Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put limit on django orm update query with order by

I have to put limit on the number of records to update in Django ORM Model object.

I have tried:

CustomObject.objects.filter(column_x='XXX').update(column_y='YYY')[:10]

but slicing is not allowed with update.

I don't want to fetch the id separately and using those ids for update as the number of records is very large ( 1 million to 80 million ).

Interested in the hitting the DB once.

like image 761
Ankur Bansal Avatar asked Mar 21 '23 08:03

Ankur Bansal


1 Answers

Use nested querying:

nested_q = CustomObject.objects.filter(column_x='XXX')[:10]
CustomObject.objects.filter(pk__in=nested_q).update(column_y='YYY')
like image 117
bphoenix Avatar answered Apr 19 '23 12:04

bphoenix