轻松地将示例数据与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-superuser

creates 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每次运行时都添加相同的数据!

您可以通过两种方式解决此问题:

  1. (建议)添加make目标或类似的内容(我们使用结构 操作)重置示例数据库并重新运行addexampledata。使用 每当您更改/添加模型或示例数据时,都会执行此操作。
  2. 在插入新数据之前检查^ {TT14} $中的现有项。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将cassandra中的行数据转换为与列相关的嵌套json   java如何使用jcr XPath在jcr:content/@jcr:data中搜索?   java在使用openCV进行安卓开发时如何利用手机的广角镜头   java解析扩展了接口,结束了一个潜在的无限循环   位置服务的@Override方法中存在java Android应用程序错误   java本地线程的用途和需求是什么   具有左右子访问的java节点树遍历   java验证JsonWebToken签名   JUL日志处理程序中的java日志记录   嵌入式Java读取给定时间段的串行数据。   java有没有办法从多个URL获取多个图像?   java线程通过等待intent阻止自己发送intent   java Spring MVC解析多部分内容请求   java JPA/Hibernate静态元模型属性未填充NullPointerException   java格式错误的字符(需要引号,得到I)~正在处理   java为什么PrintWriter对象抛出FileNotFoundException?   java Neo4j未正确保存标签   java IE不加载图像