Search code examples
pythondjangoprimary-keydjango-south

How to rename a custom ID field in django to the default


A long while back, I designed a few Django models with fields like:

documentUUID = models.AutoField("a unique ID for each document",
                                    primary_key=True)

I thought this was good -- I was being explicit about things -- but I've since realized that I'd love to purge my code of these references, and move towards a more standard implementation.

My goals are thus:

  1. Rename the field in the DB so that it's as if I never had the custom primary_key field.
  2. Remove these field definitions from my models so the default takes over.
  3. Refactor these values out of my code.

Solution

  • After I asked this I was forced by the code to do some experimentation.

    Looks like what you can do is simply:

    1. Remove the field from the model
    2. Generate the schema migration.
    3. Edit the migration to rename the field to id (db.rename_column('Document', 'documentUUID', 'id'))
    4. Migrate it and update all your code with a mondo find and replace.