Sabr
Sabr

Reputation: 193

How to get model's objects one by one in Django

I need to make a function that will be launched in celery and will take records from the model in turn, check something and write data to another model with onetoone relationship. There are a lot of entries and using model_name.objects.all () is not appropriate (it will take a lot of memory and time) how to do it correctly.

Upvotes: 0

Views: 52

Answers (1)

Gaurav Waghmare
Gaurav Waghmare

Reputation: 404

You can use an iterator over the queryset https://docs.djangoproject.com/en/dev/ref/models/querysets/#iterator so your records are fetched on by one

model_iterator = your_model.objects.all().iterator()
for record in model_iterator:
    do_something(record)

Upvotes: 1

Related Questions