如何在django中使db dumpfile [英] How to make db dumpfile in django
问题描述
请让我知道如何使用dumpdata或任何其他命令或api将文件转储到文件。
谢谢
您可以选择一个文件将dumpdata的输出置于Python使用 call_command
,例如:
from django.core.management import call_command
pre>
output = open(output_filename,'w')#将stdout指向用于转储数据的文件。
call_command('dumpdata','model_name',format ='json',indent = 3,stdout = output)
output.close()
但是,如果您尝试使用例如从命令行调用此命令在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 errormanage.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屋!