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

时间:2022-09-20 14:02:21

Is there an easy way to load fixture data that I usually use in automated test runs in the interactive Django shell?

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

It might be awkward to have a mixture of model data that come from the database and others that come from a fixture. In my case, I have some read-only tables and wand to experiment with some data that I can discard afterwards.

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

I can probably load the fixture files like described here, but that's a bit cumbersome for repeated use...

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

3 个解决方案

#1


4  

ilardm's answer points in the right direction, specifically what you want is:

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

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

Edit: But the correct way to include fixtures in test cases is like this:

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

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

    def setUp(self):
        # Ready to test

#2


1  

Perhaps this link: http://testedwebdev.blogspot.ru/2012/05/django-shell-testing.html might help.

也许这个链接:http://testedwebdev.blogspot.ru/2012/05/django-shell-testing.html可能有所帮助。

#3


0  

I expect ./manage.py loaddata fixture_name.json is what you want.

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

#1


4  

ilardm's answer points in the right direction, specifically what you want is:

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

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

Edit: But the correct way to include fixtures in test cases is like this:

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

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

    def setUp(self):
        # Ready to test

#2


1  

Perhaps this link: http://testedwebdev.blogspot.ru/2012/05/django-shell-testing.html might help.

也许这个链接:http://testedwebdev.blogspot.ru/2012/05/django-shell-testing.html可能有所帮助。

#3


0  

I expect ./manage.py loaddata fixture_name.json is what you want.

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