django database delete specific number of entries

Question:

How to delete specific number of entries from the database?
I did something like this

EntriesToDelete=Statusmessages.objects.filter(time__lt=date)[:30000]
EntriesToDelete.delete()

But I get an error which says:
AssertionError. Cannot use 'limit' or 'offset' with delete.

How can I specify the number of entries to be deleted.

Asked By: arjun

||

Answers:

You could do it like this:

Statusmessages.objects.filter(pk__in=Statusmessages.objects.filter(time__lt=date).values_list('pk')[:30000]).delete()
Answered By: jpic

use this:

Statusmessages.objects.filter(pk__in=Statusmessages.objects.filter(time__lt=date).values_list('pk', flat=True)[:30000]).delete()

use flat=True, if not then is a tuple and raise exception:

{NotSupportedError}(1235, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'")
Answered By: ChenDehua
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.