Django shell:加载测试夹具数据的命令?

时间:2012-12-17 15:15:50

标签: django shell

是否有一种简单的方法来加载我通常在交互式Django shell中自动测试运行中使用的夹具数据?

混合使用来自数据库的模型数据和来自夹具的其他模型数据可能会很尴尬。在我的情况下,我有一些只读表和魔杖来试验我之后可以丢弃的一些数据。

我可以加载像here描述的夹具文件,但重复使用有点麻烦......

3 个答案:

答案 0 :(得分:6)

ilardm的答案指向了正确的方向,特别是你想要的是:

from django.core.management import call_command
call_command('loaddata', 'fixture_name.json')

编辑:但是在测试用例中包含灯具的正确方法是这样的:

class TestThis(TestCase):
    fixtures = ['myfixture.json']

    def setUp(self):
        # Ready to test

答案 1 :(得分:1)

答案 2 :(得分:0)

我希望./manage.py loaddata fixture_name.json是你想要的。