Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Well something like
import itertools
_ids = [u.histories.order_by('-datetime_open').values_list('pk', flat=True)[:20] for u in User.objects.all()]
last_20_history_ids = list(itertools.chain.from_iterable(_ids))
History.objects.exclude(pk__in=last_20_history_ids).delete()
I would do it with one SQL query.
DELETE FROM my_history_table t
WHERE t.id <= (
SELECT t2.id
FROM my_history_table t2
ORDER BY datetime_open DESC
SKIP 20
LIMIT 1
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question