轻松地将示例数据与django应用程序捆绑在一起。
django_addexampledata的Python项目详细描述
轻松地将示例数据与django应用程序捆绑在一起。
安装
$ pip install django_addexampledata
设置
将'django_addexampledata'添加到INSTALLED_APPS。
用法
django_addexampledata提供两个django管理命令:
- addexampledata
- Finds all apps with an ^{tt4}$, and runs ^{tt5}$ on these apps. See the howto below for more info.
- createexampleuser
Create an example user. See ^{tt6}$ for the User attributes that can be set. Example:
python manange.py createexampleuser grandma --is-superusercreates a superuser named grandma with password set to ^{tt7}$.
addexampledata如何
将exampledata.py添加到django应用程序的根目录(与 models.py)。向该模块添加add_exampledata函数。你的 exampledata.py可能看起来像这样:
from models import Page def add_exampledata(): aboutus = Page.objects.create(title="About us", body="Hello cruel world") contactinfo = Page.objects.create(title="Contact info", body="Coming soon.", parent=aboutus)
只要你的django应用程序在已安装的应用程序中, <appname>.exampledata.add_exampledata每次运行时都会运行:
python manage.py addexampledata
特定应用程序上的addexampledata
您可以指定要为其添加示例数据作为参数的应用程序 命令。示例:
python manage.py addexampledata myapp myotherapp
addexampledata每次运行时都添加相同的数据!
您可以通过两种方式解决此问题:
- (建议)添加make目标或类似的内容(我们使用结构 操作)重置示例数据库并重新运行addexampledata。使用 每当您更改/添加模型或示例数据时,都会执行此操作。