ORM from the command line

37 views
Skip to first unread message

Tim Johnson

unread,
Apr 8, 2020, 6:19:19 PM4/8/20
to MLDjango
using django.VERSION (2, 1, 5, 'final', 0) with

python 3.7.2 on ubuntu 16.04

I have a need for a "Housekeeping" application. It's usage would be to

1) connect to a database, either mysql, mariadb or postgres

2) truncate two tables and repopulate them based on an arbitrary data
structure such as a compound list or tuple.

Such an application would not need be and most preferably should not be
part of a deployed website.

This should not be a very complicated endeavor. The simplest method
might be to manually establish an ORM connection using settings.py to
import the connection credentials. I am wondering if this is possible.

However, I am unable to find documentation that would edify me on
manually coding an ORM connection and clearing a database without the
loading of django resources.

If such an approach is feasible, I would welcome URLs to appropriate
documentation and/or discussion.

Using a model-view approach would be the simplest method, I would think,
but there would be no need to have such a view deployed. There is
probably a solution that would necessitate installing a custom package
to be used from manage.py, such as
https://github.com/KhaledElAnsari/django-truncate and that might be
complicated.

Comments are welcome

thanks

--
Tim
tj49.com

Andréas Kühne

unread,
Apr 9, 2020, 6:06:46 AM4/9/20
to django...@googlegroups.com
Hi Tim,

What you probably should do is use a custom command on the manage.py command interface. You till then get access to all of djangos goodness - and it can be run from the command line.

See here:

This is how I would handle it.

Regards,

Andréas


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4be7c6fc-d99e-7419-24ef-d54cbdf384d4%40akwebsoft.com.

Tim Johnson

unread,
Apr 9, 2020, 11:15:46 AM4/9/20
to django...@googlegroups.com

Thank you Andréas. I have come across that too, after my OT.

This is definitely what I was looking for.

cheers

Adam Mičuda

unread,
Apr 9, 2020, 11:46:58 AM4/9/20
to django...@googlegroups.com
Hi,
you can also write simple python script and run it in Django context via https://django-extensions.readthedocs.io/en/latest/runscript.html from `django-extensions` package.

Best regards.
Adam

čt 9. 4. 2020 v 17:14 odesílatel Tim Johnson <t...@akwebsoft.com> napsal:

Tim Johnson

unread,
Apr 9, 2020, 1:40:39 PM4/9/20
to django...@googlegroups.com

Thank you too Adam.

Great stuff!

Reply all
Reply to author
Forward
0 new messages