python - Django deleting querysets with paging, not catching all parts of the set -
i have bit of strange problem i'm not quite able explain.
i have django project old, stale objects lying around. example, lets objects this:
class blog_post(models.model): user_account = models.foreignkey('accounts.account') text = models.charfield(max_length=255) authors = models.manytomanyfield(author) created = models.datetimefield(blank=true, null=true)
this not exact copy of model, close enough.
i've created management command build ordered querysets of these objects, , delete with paginator
my command looks this:
all_accounts = account.objects.all() act in all_accounts.iterator(): stale_objects = blog_post.objects.filter(user_account=act, created=django.utils.timezone.now() - datetime.timedelta(days=7)) paginator = paginator(stale_objects.order_by('id'), 100) page in range(1, paginator.num_pages + 1): page_stale_objects = blog_post.objects.filter(id__in=paginator.page(page).object_list.values_list('id')) page_stale_objects.delete()
the problem i'm having is, after delete these objects command, there still objects fit queryset parameters not deleted. so, have run command 3+ times find , remove objects.
i first figured date range weirdly on edge of datetime not catching objects made shortly after 1 week past command time. not case, i've removed created=... filter queryset, , have same results.
why querysets not catching objects first time command runs? there not excessive objects, @ ~30,000 rows.
paging through queryset translated consecutive limit/offset calls. so, think sequence:
- get items offset 0 , limit 20
- delete items
- get next page, ie 20 items offset 21
but wait! once we've deleted first set, queryset starts @ 0 again. items 0 20 skipped.
the solution is, don't this. pagination displaying objects, not deleting them.
Comments
Post a Comment