Django Shell:加载测试夹具数据的命令? [英] Django shell: Command to load test fixture data?

查看:34
本文介绍了Django Shell:加载测试夹具数据的命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there an easy way to load fixture data that I usually use in automated test runs in the interactive 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...

推荐答案

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

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

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

但是在测试用例中包含固定装置的正确方法是这样的:

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

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

    def setUp(self):
        # Ready to test

这篇关于Django Shell:加载测试夹具数据的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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