Hi, everyone! I am handling this problem. I should forward all recordings from one model to another. There I have about 2 million recordings with about 20 columns. Here’s my code:
def forward(frm, to, model, apps, schema_editor):
old_model = apps.get_model(frm, model) new_model = apps.get_model(to, model) list_fields = [i.get_attname() for i in old_model._meta.fields] qs = old_model.objects.all().order_by('created') new_model.objects.all().delete() i = 0 j = 10000 q = 0 no = new_model() with transaction.atomic(): while qs.last() != q: qs_part = qs[i:j] for item in qs_part: for field in list_fields: val = getattr(item, field) setattr(no, field, val) no.save() print("{} in {} forwarded".format(qs_part.count(), model)) q = qs_part[qs_part.count()-1] i += j j += j print("{} is ok at {}".format(model, datetime.datetime.now()))
Every time I get this error message:
return self.cursor.execute(sql, params)
psycopg2.OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.