Cannot access database API in Django - python

I am working on the following tutorial on Django (https://docs.djangoproject.com/en/1.4/intro/tutorial01/).
When I invoke the interactive shell to access the database API using the command
python manage.py shell, I receive the following prompt
In [1]:
Please can someone tell me what this means and how I can access the database API.
Thanks

That's the normal shell prompt, I don't think it's a bug, maybe you want to check the commands offered by manage.py because I think you wanted to do something else.

Related

Django: Run a script right after runserver

Context:
I have a table on the database that uses values from an external database. This external database updates its values periodically.
Problem:
In order to update my database everytime i start the server, I want to run a script right after the runserver.
Potential Solution:
I have seen that it is possible to run a script from a certain app, which is something I'm interested in. This is achievable by using the django-extensions:
https://django-extensions.readthedocs.io/en/latest/runscript.html
However, this script only runs with the following command:
python manage.py runscript your_script
Is there any other way to run a script from an app and execute it right after the runserver command? I am open to suggestions!
Thanks in advance
Update
Thanks to #Raydel Miranda for the remarks, I feel i left some information behind.
My goal is, once I start the server I'm planning to open a socket to maintain my database updated.
You can execute the code in the top-level urls.py. That module is imported and executed once.
urls.py
from django.confs.urls.defaults import *
from your_script import one_time_startup_function
urlpatterns = ...
one_time_startup_function()
I would recommend to use something like this, lets say you have the script like this:
# abc.py
from your_app.models import do_something
do_something()
Now you can run this script right after runserver(or any other way you are running the django application) like this:
python manage.py runserver & python manage.py shell < abc.py
FYI, it will only work if you have bash in your terminal (like in ie Linux, MacOs).
Update
After reading you problem carefully, I think running a script after runserver might not be the best solution. As you said:
This external database updates its values periodically.
So, I think you need some sort of perodic task to do this update. You can use cronjob or you can use Celery for this.
Running the script after runserver don't seem a very good idea, the main reason is that you will have a window since the server is running (and available for users) till you finish synchronizing your data. Also if you synchronize using a script after runserver you won't get updates from the external db after that.
The best solution for this is to configure multiple databases, you can use the external database with only read access. This way your views will provide really updated data.
On the other hand ...
If want use something like a script is better to write a Django custom command (this way you don't have to deal with initializing django settings and other issues) and execute it using cron or celery as #ruddra states in his/her answer.
Said this, you should see this: https://docs.djangoproject.com/en/2.1/topics/db/multi-db/
This may help.
you can edit yourapp/apps.py
class MyAppConfig(AppConfig):
name = 'myapp'
def ready(self):
# update my database here
pass

Profiling Django with PyCharm

My app is pretty slow even in a dev environment so I would like to figure out what's slowing it down so I can try and fix it.
I know about the debug toolbar and according to what it reports neither the database queries nor the downloaded sources are the issue, so it must be the business logic.
However, I cannot run the PyCharm profiler with the Django server because, well, it's not like running a script.
How may I profile Django with PyCharm?
You should probably set configuration properly.
Then click on Edit Configurations...
The main thing is to set Interpreter (your virtual environment). You don't have to set Custom Run Command if you use python manage.py runserver
Then you can run Django server directly from PyCharm ann Profiler too.

Is it possible to manage model data without running the server?

Suppose I have a python module written to do some clean job and daily maintenance. It has no view or template but simply a command line tool. Is it possible to interact with the models and db regardless of whether the server is on?
Yes you can.
Look into the management commands shell and dbshell
You would just do
python manage.py shell #You can call any method, modify Model objects, ...
and
python manage.py dbshell #Gives direct access to the database via command line
And this does not need the server to be running.

GAE - Online admin console

Is there some way I can run custom python code on my google appengine app online? Is there a python console somewhere that I can use? I've seen vague references here and there, but nothing concrete.
Check out these previous answers on how to enable the interactive console (that you can use on the local dev appserver) on your deployed application.
You can use remote shell, that is on your app engine sdk.
For example
~/bin/google_appengine/remote_api_shell.py -s your-app-identifier.appspot.com s~your-app-identifier
When you are inside the shell, you will have the db module enabled. in order to use your models, you will have to import them.

Calling an executable from within Python / Django web application running on IIS

I have a Python / Django application which is supposed to call an external windows binary and get its output at some point. And it does so when tested via 'python manage.py shell'.
But when it is run from within the web browser, which is served by IIS, the external application is not executed.
Is IIS blocking something on the way? Can this be avoided?
Any help is much appreciated.
oMat
Might be a permissions issue. when you run from the shell, you're using the user that run the python manage.py shell command. When serving requests from the IIS you're using its user (IUSR or something like that). Try giving execution permission on the executable file to the Everyone group just to see if it helps.

Categories

Resources