如何将django中的自定义ID字段重命名为缺省值

时间:2022-07-08 19:35:30

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

很久以前,我设计了一些Django模型,其领域如下:

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. 在DB中重命名字段,这样就好像我从来没有自定义的primary_key字段一样。
  3. Remove these field definitions from my models so the default takes over.
  4. 从我的模型中删除这些字段定义,以便默认值接管。
  5. Refactor these values out of my code.
  6. 从代码中重构这些值。

2 个解决方案

#1


2  

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. 从模型中删除字段
  3. Generate the schema migration.
  4. 生成模式迁移。
  5. Edit the migration to rename the field to id (db.rename_column('Document', 'documentUUID', 'id'))
  6. 编辑迁移,将字段重命名为id (db)。rename_column(“文档”、“documentUUID”、“id”))
  7. Migrate it and update all your code with a mondo find and replace.
  8. 迁移它并使用mondo查找和替换更新所有代码。

#2


0  

Have you looked at south? It is a tool created for precisely this problem.

你看过《南方》吗?它正是为这个问题而创建的工具。

#1


2  

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. 从模型中删除字段
  3. Generate the schema migration.
  4. 生成模式迁移。
  5. Edit the migration to rename the field to id (db.rename_column('Document', 'documentUUID', 'id'))
  6. 编辑迁移,将字段重命名为id (db)。rename_column(“文档”、“documentUUID”、“id”))
  7. Migrate it and update all your code with a mondo find and replace.
  8. 迁移它并使用mondo查找和替换更新所有代码。

#2


0  

Have you looked at south? It is a tool created for precisely this problem.

你看过《南方》吗?它正是为这个问题而创建的工具。