Django Migrations Add Field with Default as Function of Model

前端 未结 3 563
萌比男神i
萌比男神i 2020-12-02 09:31

I added a new, non-nullable field to my Django model and am trying to use migrations to deploy that change. How would I set default value to use for existing models to be so

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 10:14

    You should also define a reverse for your function set_my_defaults(), in case you what to revert the migration in the future.

    def reverse_set_default(apps, schema_editor):
        pass
    

    The reverse function in this case need to do nothing, since you are removing the field.

    And add it to RunPython:

    migrations.RunPython(set_my_defaults, reverse_set_default),
    

提交回复
热议问题