custom command

25 views
Skip to first unread message

dp Audiovisual

unread,
Jul 20, 2018, 7:37:42 AM7/20/18
to Django users
Hello, I need to create a "custom command" that uploads all the Json files of a folder within the project to the postgres database. Help, I'm learning.

Julio Biason

unread,
Jul 20, 2018, 7:55:19 AM7/20/18
to django...@googlegroups.com
Hi Dp,

Basically, you'll have to:

1. Add the command; the documentation explains quite well how to make a new command: https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/

2. In that command, you'll have to read a directory. That's pure python and you could use something like walk https://docs.python.org/3/library/os.html?highlight=os#os.walk, although there are other commands. Check the `os` and `os.path` modules.

3. Reading a JSON is easy peasy with the JSON module: https://docs.python.org/3/library/json.html?highlight=json#module-json

4. And to add the values in PostgreSQL, you'll need to have your model defined (https://docs.djangoproject.com/en/2.0/topics/db/models/) and, with that, you just create records and `.save()` them.



On Fri, Jul 20, 2018 at 1:24 AM, dp Audiovisual <dgp...@gmail.com> wrote:
Hello, I need to create a "custom command" that uploads all the Json files of a folder within the project to the postgres database. Help, I'm learning.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6f2c4d6a-8f05-44b9-967c-c3fe0f7f5e72%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554

Mikhailo Keda

unread,
Jul 20, 2018, 11:21:57 AM7/20/18
to Django users
check this example - https://bitbucket.org/voron-raven/maps/src/master/core/management/commands/import.py

пʼятниця, 20 липня 2018 р. 14:37:42 UTC+3 користувач dp Audiovisual написав:
Reply all
Reply to author
Forward
0 new messages