how to execute the interactive commands programmatically in django-tenants - django

in my terminal i can execute
1)first command
python manage.py tenant_command rebuild_index
2)second command.
the terminal will ask which schema shall i execute. for that i enter my schema name called xxx
Enter Tenant Schema ('?' to list schemas): xxx
3)3rd command i need to enter y/n option extra in terminal
then i Entered Enter
This is working fine so how to achieve this in pragmatically using management call_command pragmatically in django
management.call_command('python manage.py tenant_command rebuild_index', 'xxx')
but it giving error like
File "/home/hi/venv/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 94, in call_command
raise CommandError("Unknown command: %r" % name)
CommandError: Unknown command: 'python manage.py tenant_command rebuild_index'
so please any body tell me is it possible to achieve this kind of interactive terminal commands to run pragmatically in django

You need to pass the schema name as a keyword argument called schema_name so that the tenant_command does not prompt. You also do not need to pass python manage.py.
The syntax you want is:
from django.core import management
management.call_command('tenant_command', 'rebuild_index', schema_name='xxx')

The function call_command only needs the command name, like this:
management.call_command('tenant_command', 'rebuild_index')
Try that and see if that works. Read the docs I linked as well, there are a few examples.

I solved my problem by running it in a periodic task instead of running manual rebuild index commands everytime. See code below...
from django.core import management
def hi():
management.call_command('rebuild_index',schema_name="xxx",interactive=False)

Related

How would I go about creating a gui to run a django project?

I have a Django project and I would like to create a simple python GUI to allow users to change the host address and port on the go without having to interact with a command prompt. I already have a simple python user interface but how would I go about coding something in python that would be able to run a command such as python manage.py createsuperuser and fill in the required information without a need to run it in a python script that just calls regular terminal commands such as:
subprocess.call(["python","manage.py", "createsuperuser"])
I don't know if this is something one can do without commands but would be amazing if it is since then I can just implement it into my basic python GUI that would allow users to createsuperuser, runserver, makemigrations, migrate and even change the default host and port on the go.
you can use from this code and change each command you want , pay attention if you run this command your shell must be in your django project folder
if you want, you can change direction with "CD" command before django run command
from subprocess import Popen
from sys import stdout, stdin, stderr
Popen('python manage.py runserver', shell=True, stdin=stdin, stdout=stdout ,stderr=stderr)
you can communicate with shell by this code :
from subprocess import Popen, PIPE
from sys import stdout, stdin, stderr
process = Popen('python manage.py createsuperuser', shell=True, stdin=PIPE, stdout=PIPE ,stderr=stderr)
outs, errs = process.communicate(timeout=15)
print(outs)
username="username"
process.stdin.write(username.encode('ascii'))
process.stdin.close
Unfortunately, for createsuperuser you get this error :
Superuser creation skipped due to not running in a TTY. You can run manage.py createsuperuser in your project to create one manually
You can't create super user with tty for security problems.
I prefer :
you can use from this code in your project for create superuser
from django.contrib.auth.models import User;
User.objects.create_superuser('admin', 'admin#example.com', 'pass')
if you want create super user with shell I would suggest running a Data Migration stackoverflow.com/a/53555252/9533909

How can i use django's runscript?

I am trying to run a script using django's runscript. I followed everything in the documentation. Did i miss something?
But when i tried running it from the command line. it says unknown command 'runscript'
(env) C:\Users\MIS\hr system\hr_project>python manage.py runscript automail.py
Unknown command: 'runscript'
You need to install django-extensions if you want the runscript command. If you don't want to do that, you can:
Run your script directly. Keep in mind that you need to specify the Django settings module as such:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings")
from your_project.models import SomeModel
# Your code goes here...
Make a custom manage.py command. You can use the official how-to: https://docs.djangoproject.com/en/2.2/howto/custom-management-commands/

Cron Job that access django Models

What i have done?
I have written a small application in django with few models with sqlite3 as backend. Now i want to write a python code that clears the database elements based on certain condition.
Question:
How can i achieve the above requirement?
I think the cleanest way to do this is to write your own django-admin command.
You can then run the command using manage.py:
python manage.py your_command
Having a command that can be run in shell, you can easily put in into your crontab. Optionally django-admin commands can receive command line arguments, if needed.

How to run a series of manage.py commands in Django?

I have a Django project. Everytime I deploy, I need to run a series of manage.py command (such as syncdb, south migiration, fixture update).
I am getting tired of type the command line by line and therefore I wrote a python script to do these:
import subprocess
subprocess.call(['python', 'manage.py', 'syncdb'])
#Skip the detail
subprocess.call(['python', 'manage.py', 'loaddata', 'setup/fixture.xml'])
I am wondering if there is a better way to do this?
Thanks.
You can use fabric, a Python library that allows you to script remote actions. This question has some links in the accepted answer for more information on fabric and django.
You can also call management commands directly:
from django.core.management import call_command
call_command('syncdb')
call_command('loaddata', 'setup/fixture.xml')
Save that as a normal python file and execute it from your shell or as part of your deployment scripts.

Run custom admin command from view

I have a custom admin command that emails out reports. It normally runs from a cron job. What I would like to do is add a button to my web app that when clicked will cause the the admin command to run there and then rather than wait for the cron job to call it. How do I do this? Do I have to call out to a command line such as
python manage.py myadmincmd
or can I invoke the code from within a view? It seems it would be cleaner if I could do this from within a view without needing to break out to the command line.
You can use call_command:
from django.core.management import call_command
call_command('myadmincmd')