Skip to content

Commit

Permalink
Expose batch_qs to plugin API
Browse files Browse the repository at this point in the history
closes #4607
  • Loading branch information
quba42 authored and mdellweg committed Nov 2, 2023
1 parent 2e8d45e commit 268fc3a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES/plugin_api/4607.feature
@@ -0,0 +1 @@
Exposed a function to return a batch from a given queryset at ``pulpcore.plugin.util.batch_qs``.
24 changes: 15 additions & 9 deletions pulpcore/app/util.py
Expand Up @@ -180,15 +180,21 @@ def get_view_name_for_model(model_obj, view_action):


def batch_qs(qs, batch_size=1000):
"""
Returns a queryset batch in the given queryset.
Usage:
# Make sure to order your querset
article_qs = Article.objects.order_by('id')
for qs in batch_qs(article_qs):
for article in qs:
print article.body
"""Returns a queryset batch from the given queryset.
Make sure to order the queryset.
Args:
qs: The queryset we want to iterate over in batches.
batch_size: Defaults to 1000.
Example:
To iterate over a queryset while retrieving records from the DB in batches, use::
article_qs = Article.objects.order_by('id')
for qs in batch_qs(article_qs):
for article in qs:
print article.body
"""
total = qs.count()
for start in range(0, total, batch_size):
Expand Down
1 change: 1 addition & 0 deletions pulpcore/plugin/util.py
Expand Up @@ -13,6 +13,7 @@
)

from pulpcore.app.util import ( # noqa: F401
batch_qs,
extract_pk,
get_artifact_url,
get_url,
Expand Down

0 comments on commit 268fc3a

Please sign in to comment.