djangodjango-modelsdjango-ormdjango-migrations

How to define default data for Django Models?


I want my application to have default data such as user types.

What's the most efficient way to manage default data after migrations?

It needs to handle situations such as, after I add a new table, it adds the default data for it.


Solution

  • Update:

    most users are looking for data migration as suggested by @durdenk in https://stackoverflow.com/a/39742847/3627387. But what OP was asking is about a way to add data after migrations, that is why this is accepted answer.

    Original answer:

    I think what you are looking for is fixtures https://docs.djangoproject.com/en/1.10/howto/initial-data/

    From docs

    It’s sometimes useful to pre-populate your database with hard-coded data when you’re first setting up an app. You can provide initial data via fixtures.

    Also read this https://code.djangoproject.com/wiki/Fixtures