如何从所有应用程序加载Django灯具? [英] How to load Django fixtures from all apps?

查看:134
本文介绍了如何从所有应用程序加载Django灯具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我使用--verbosity = 2手动运行loaddata时,我可以在Django应用程序中使用固定装置,但只有两个应用程序正在加载它们的装置。看到它只看到两个应用程序,虽然我有更多的夹具目录创建里面。



所有应用程序都正确安装在settings.py。



从文档中可以看出,Django应该在每个安装的应用程序的fixtures /目录中搜索。



忽略?

解决方案

每次执行syncdb时,都会导入Initial_data。像我记得的一样公平,它也会覆盖您手动完成的任何更改。



要加载其他灯具,您必须使用manage.py loaddata fixturename。
如果您在所有应用程序中使用了一个常见的命名方案,那么效果很好。
如果没有,你必须给loaddata一个名字,或者使用find来获取
每个其中的fixtures和exec loaddata的列表:



编辑:(当我安装django软件包时,我将manage.py添加到virtualenv中,我只使用manage.py,如果你不需要python manage.py loaddata课程)

  find。 -name* .json-exec manage.py loaddata {} \; 

我在fabfile中使用这个来自动执行分段安装:

  def load_all_fixtures():
加载每个目录中的所有夹具
与cd(env.directory):
run(
source /usr/local/bin/virtualenvwrapper.sh&&b $ b workon%s&&
find -L。-name*。 json-exec manage.py loaddata {} \;

%env.virtualenv)


I'm using fixtures in my Django application but only two apps are getting their fixtures loaded.

When I manually run loaddata with --verbosity=2 I can see that it only looks in two apps although I have more with fixtures directories created inside.

All apps are correctly installed in settings.py.

From the documentation it seems that Django is supposed to search in the fixtures/ directory of every installed application.

Any ideas why some apps are ignored ?

解决方案

Initial_data gets imported each time you do syncdb. As fair as I remember, it also overwrites any changes you have done manually.

To load other fixtures, you have to use manage.py loaddata fixturename. That works well if you have you use a common naming scheme in all your apps. If you don't, you have to give loaddata the name of each one, or use find to get the list of fixtures and exec loaddata in each one of them:

EDIT: (as I add manage.py to /bin in the virtualenv when I install the django package I only use manage.py, if you don't you will need python manage.py loaddata of course)

find . -name "*.json" -exec manage.py loaddata {} \;

I use this in a fabfile to automate staging installations:

def load_all_fixtures():
    """Loads all the fixtures in every dir"""
    with cd(env.directory):
        run("""
            source /usr/local/bin/virtualenvwrapper.sh &&
            workon %s &&
            find -L . -name "*.json" -exec manage.py loaddata {} \;

            """ % env.virtualenv )

这篇关于如何从所有应用程序加载Django灯具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆