A
A
Alexey2017-06-16 09:13:45
Django
Alexey, 2017-06-16 09:13:45

How to pass stdout data to stringio?

There is a custom command that calls the dumpdata command. It is necessary to transfer the returned data from this call to stringio, how can I do this?

fake_file = io.StringIO()
call_command('dumpdata', stdout=fake_file)
print(fake_file.read()) #Ноль эффекта

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2017-06-16
@SamarNevil

Well, why these crutches? If you need to get json of all project data, use a serializer, as the dumpdata command itself does:

from django.apps import apps
from django.core import serializers

# Получаем список всех моделей всех приложений из INSTALLED_APPS
# и фильтруем django'вские
models = [model for model in apps.get_models() if not model.__module__.startswith('django')]

# Сериалзиуем в json полный QuerySet каждой модели
dump = ''
for m in models:
    dump += serializers.serialize('json', m._default_manager.all())

E
Eugene, 2017-06-16
@immaculate

import subprocess
data = subprocess.check_output('dumpdata')
print(data)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question