Django South - Create Not Null ForeignKey

前端 未结 3 1594
抹茶落季
抹茶落季 2021-02-06 16:09

I have a Model

class Mystery(models.Model):
    first = models.CharField(max_length=256)
    second = models.CharField(max_length=256)
    third = models.CharFie         


        
3条回答
  •  梦毁少年i
    2021-02-06 16:42

    This is best accomplished in 3 migrations.

    Step 1. Create the new model and allow player to be null: player = models.ForeignKey(Player, null=True)

    Step 2. Run ./manage.py schemamigration --auto

    Step 3. Run ./manage.py datamigration set_default_players

    Step 4. Update forwards and backwards in /migrations/_set_default_players.py to use whatever logic you like for setting the default player. Ensure that every Mystery object has a value for player.

    Step 5. Update the Mystery model so that player has null=False.

    Step 6. Run ./manage.py schemamigration --auto

    Step 7. Run ./manage.py migrate

提交回复
热议问题