1👍
First of all you can change the timeout time of MySQL by edditing settings.py
DATABASES = {
'default': {
...
OPTIONS = {
'connect_timeout': 5, # your timeout time
}
...
}
}
The reasons why .delete()
may be slow are:
- Django has to ensure cascade deleting functions properly. It has to look for foreign keys to your models
- Django has to somehow handle pre and post-save signals for your models
If you are sure that your models don’t have cascade deleting or any signals to be handled, you can try to use private _raw_delete
as follows:
queryset._raw_delete(queryset.db)
You can find more details on it here
1👍
Finally I got a simple solution to delete all cascade object breaking down the macro deletion operation preferring delete Fund objects one by one. Because it is a really long operation (approximately 1 second for each object for thousands objects) I assigned it to a celery worker. Slow but safe I think, if someone got a better solution please let me know!
@shared_task
def reset_funds():
for fund in Fund.objects.all():
print "Delete Fund: {}".format(fund.name)
fund.delete()
return "All Funds Deleted!"
Source:stackexchange.com