[Answer]-How to query and update some record set in django models atomically?

1👍

Just:

Model.objects.filter(...).update(count=F('count') + 1)

Here is the doc.

If you want to limit the size of QuerySet as 100, use Model.objects.filter(...)[:100]. It will be translated to SELECT ... LIMIT 100, so you don’t need to worry about the performance, here is another question about this.

And it’s return value is <class 'django.db.models.query.QuerySet'>, same as Model.objects.filter(...), so you can use Model.objects.filter(...)[:100].update(...) too.

Leave a comment