如何在django中使db dumpfile [英] How to make db dumpfile in django

查看:154
本文介绍了如何在django中使db dumpfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在django中进行转储,不管​​我使用的数据库是什么,可以稍后加载。命令'dumpdata'是完美的,但它是在控制台上打印输出。更多的我使用call_command函数调用它,所以我无法将其内容存储在任何变量中,因为它是在控制台上打印输出。



请让我知道如何使用dumpdata或任何其他命令或api将文件转储到文件。



谢谢

解决方案

您可以选择一个文件将dumpdata的输出置于Python使用 call_command ,例如:

  from django.core.management import call_command 

output = open(output_filename,'w')#将stdout指向用于转储数据的文件。
call_command('dumpdata','model_name',format ='json',indent = 3,stdout = output)
output.close()
pre>

但是,如果您尝试使用例如从命令行调用此命令在dumpdata命令的末尾, - stdout = filename.json 会给出错误 manage.py:error:no such option: stdout



所以在那里,你只需要在Python脚本中而不是在命令行中调用它。如果您希望将其作为命令行选项,则重定向(如其他人所建议的)是您最好的选择。


I want to make a dump in django irrespective of database I am using and can be loaded later. The command 'dumpdata' is perfect for this, but it is printing output on console. More over I am calling it using call_command function so I cannot store its content in any variable as it is printing output on console.

Please let me know how store dump to a file using dumpdata or any other command or api.

Thanks

解决方案

You can choose a file to put the output of dumpdata into if you call it from within Python using call_command, for example:

from django.core.management import call_command

output = open(output_filename,'w') # Point stdout at a file for dumping data to.
call_command('dumpdata','model_name',format='json',indent=3,stdout=output)
output.close()

However, if you try calling this from the command line with e.g. --stdout=filename.json at the end of your dumpdata command, it gives the error manage.py: error: no such option: --stdout.

So it is there, you just have to call it within a Python script rather than on the command line. If you want it as a command line option, then redirection (as others have suggested) is your best bet.

这篇关于如何在django中使db dumpfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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