2

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

Swift
  • 1,663
  • 1
  • 10
  • 21
Keso
  • 71
  • 9
  • As a side note, unrelated to your question, it is usually not required to have both a docker container and inside have a virtual environment. They're both sandboxing the installed packages to their required applications. The exception might be if you had two or more django applications running on the same docker instance, but I couldn't imagine why one would do that as opposed to having a second docker instance. – Swift Oct 25 '22 at 08:41
  • There probably is a `print` statement in your code printing that line (probably in your settings), Django doesn't normally read .env files. Also why even worry about redirecting the output etc. there's an [option](https://docs.djangoproject.com/en/4.1/ref/django-admin/#cmdoption-dumpdata-output) to specify which file the output should go to. – Abdul Aziz Barkat Oct 25 '22 at 08:43
  • @Swift are you talking about pipenv? I dont understand, it is necessary to use pipenv run with this. Or i did not understand you. – Keso Oct 25 '22 at 09:12
  • You have installed the pip env and so yes, you do need to because that's your configuration. I'm saying as a general rule, if you have a docker container, you need not configure pip env but simply install the packages to the docker containers python. – Swift Oct 25 '22 at 09:18
  • @Swift, okey, but i dont understand, why it is better option that pipenv if i am developing in this environment. In that option, pipenv is usless, everyone says that pipenv is a future. In your opinion i should instal only requirements without pipfile? – Keso Oct 25 '22 at 09:24
  • 1
    @Keso If you want to answer the question please post it as an _answer_. Don't edit the answer into the question. – Abdul Aziz Barkat Oct 25 '22 at 09:43
  • @AbdulAzizBarkat, I saw that this is a common way to do it xD. sorry it is my first question when someone helped me. – Keso Oct 25 '22 at 09:44
  • 1
    See [Can/Should I edit my question to an add answer?](https://meta.stackoverflow.com/questions/387912/can-should-i-edit-my-question-to-an-add-answer) – Abdul Aziz Barkat Oct 25 '22 at 09:46

2 Answers2

1

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)

Swift
  • 1,663
  • 1
  • 10
  • 21
  • Remeber that, if you are using pipenv , you HAVE to use pipenv run python manage.py ... I see, that if i am inside a docker by your option file is generating without this stdout mentioned in my question. But it is imposible to write a sh script with this method. I don't want to dumpdata everytime i run docker-compose, so i dont understand your 4th line. I will test your suggestion with -o flag. If it works, i will add it to my question edit. – Keso Oct 25 '22 at 09:19
  • 1
    My initial response was a solution you could manually run in a command prompt to get the output you desire. Please note my recommended solution is the `-o` flag as this is more explicit. – Swift Oct 25 '22 at 09:21
  • Okey, i understand. Your solution is right, but let me explain what i did in question edit. Thanks a lot, I will give you mark for answer but everyone who read this, check question edit. Thanks again. – Keso Oct 25 '22 at 09:36
0

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

Keso
  • 71
  • 9
  • This should be the accepted answer, though its worth noting that the difference between django-admin and manage.py is only that one must be invoked within the project, where as django-admin can be invoked from any directory because it requires you to set the env variables so it knows where to find the project settings. – Swift Oct 25 '22 at 10:06
  • Yea, but i can accept my answer only after 2 days – Keso Oct 25 '22 at 12:32