Home > Enterprise >  Django dumpdata prepending additional data to json export
Django dumpdata prepending additional data to json export

Time:10-25

I have a question how to generate dumpdata without this txt on start:

[1mLoading .env environment variables...[0m

Here is an example:

[1mLoading .env environment variables...[0m
[
 {
  "model": "auth.permission",
  "pk": 1,
  "fields": {
   "name": "Can add permission",
   "content_type": 1,
   "codename": "add_permission"
   }
 },
....

I can't find solution, it is annoying, because i want to do a sh script

docker-compose exec django pipenv run python manage.py dumpdata --indent 2 > fixtures/test_dumpdata.json

CodePudding user response:

As one of the comments mentioned, you can also completely bypass using stdout redirection by using the -o or --output flags, providing a valid path and filename as the flags parameter. And in your case this would be the RECOMMENDED way to do it.

More information on that in the docs here: https://docs.djangoproject.com/en/4.1/ref/django-admin/#cmdoption-dumpdata-output

Additionally, if you just want to do ithis one time, you can go into the docker container itself.

What is happening is its writing stdout to the file you specified, but because you're running the command from the host, there's extra verbosity from docker in the stdout.

docker exec -it <container_name> bash
python manage.py dumpdata ...

Also, in your specific case you will need to activate your virtual environment before running dumpdata

Furthermore, you may automate this by creating a script to dump data in the docker container, and invoking that from the host as youcwere before (I believe, im currently unable to test this last bit)

CodePudding user response:

Thanks for Swift. To do it i used -o flag with output path.

Here is done script.

#!/bin/bash

cd ..
docker-compose exec <container_name> pipenv run python manage.py dumpdata -o fixtures/dumpdata_test.json --indent 2

Pipenv run is important if you are using pipenv, -o OUTPUT set a destination, --indent 2 change inline to beauty json format. Swif said about django-admin, my solution is still with manage.py

  • Related