Using the south is much easier than I thought.
Use the south schema command, as usual, to create a schema migration that adds the DocumentCluster model.
Then use the datamigration command to create the skeleton for the following transition:
./manage.py datamigration yourapp populate_clusters
Then fill in the resulting method in the python migration file so that it looks like this:
def forwards(self, orm): max_id = -1 clusters_added = 0 for document in orm.Document.objects.all(): cluster = orm.DocumentCluster() cluster.id = document.id cluster.sub_document = document cluster.save() max_id = max(max_id, document.id) clusters_added +=1 if max_id >= clusters_added: orm.DocumentCluster.objects.raw("SELECT "\ "setval(pg_get_serial_sequence('yourapp_documentcluster',"\ "'id'), %d)" % max_id+1)
(The reverse method in this datamigration will simply delete all instances of the DocumentModel.)
If this populate_clusters migration step starts immediately after the transition step that adds the DocumentCluster table, the DocumentCluster table is empty and the serial / autokey counter starts from zero. If you have never deleted any documents, the value of the serial / autokey counter will end at the next unused value for Document pk values, and you wonβt even have to raise it, as shown in Erwin's answer.
If, however, you deleted Document instances, or if some id value was missing, you will need to activate this serial / autokey counter using SQL. Django provides the ability to directly execute raw SQL. (I do not think that you can do without using raw SQL).
For security reasons, you should check if cluster.id is <= the maximum value of document.id in the loop.