Ankur Bansal
Ankur Bansal

Reputation: 99

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.

Upvotes: 4

Views: 4493

Answers (2)

bphoenix
bphoenix

Reputation: 73

Use nested querying:

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

Upvotes: 7

almalki
almalki

Reputation: 4775

I don't think this is possible, as for ids, you don't have to load all records once, load a slice, updated it and then next slice. see this example.

Upvotes: 0

Related Questions