如何为 Django 模型定义默认数据? [英] How to define default data for Django Models?
问题描述
我希望我的应用程序具有默认数据,例如用户类型.
迁移后管理默认数据的最有效方法是什么?
它需要处理一些情况,例如,在我添加一个新表后,它会为其添加默认数据.
更新:
大多数用户都在寻找@durdenk 在https://stackoverflow.com/a/39742847/3627387.但是 OP 要求的是一种在迁移后添加数据的方法,这就是为什么这是公认的答案.
原答案:
我认为您正在寻找的是 fixtures
https://docs.djangoproject.com/en/1.10/howto/initial-data/
来自文档
<块引用>在您第一次设置应用程序时,用硬编码数据预先填充您的数据库有时很有用.您可以通过夹具提供初始数据.
另请阅读此https://code.djangoproject.com/wiki/Fixtures>
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.
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
这篇关于如何为 Django 模型定义默认数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!