Access Django from outside Django shell in real Python - python

I run Django through a Python 3.4 virtualenv and I want to make some cronjob scripts that will access models and save.
I can do this through the Django shell python manage.py shell will initiate the Django shell. From there I can do from polls.models import Poll, Choice and make a new Poll and save it into the DB.
How can I do this through the regular Python shell?

Your best bet is probably to create a management command. You can run it from the command line by calling (python manage.py ) and from within the script you can access any django functions or models.
For further instructions on how see here https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

Related

How to preload variables in ./manage.py shell in Django 3.0.3+?

I do most of my work through the shell in django. I'd like to preload imports and variables so as to keep myself from having to type
from app.models import *
There are multiple ways to do it. Here's a few of them.
The shell_plus command from django-extensions will automatically import your models on startup.
Django's shell will use the PYTHONSTARTUP script if you have one defined, same as the plain Python shell would. (Assuming you're using the default shell.) This is run in the same namespace as the Django shell, so if you do your imports there, they will show up in the Django shell.
IDEs that support a Django shell will allow you to define a startup script to use when the integrated Django shell is started. Check the settings. (PyCharm Pro, for example, can do this.)

Django Pinax-Project-Account github setup

So I am fairly new to Github and Django for that matter, I was digging around for a template of a website using Django offering user authentication and cam across this
pinax-project-account
I setup django and python for use within my cmd and using a virtual env and I stepped through the setup list but I'm stuck using:
chmod +x manage.py
I understand this is for use in Unix just wondering how I get the same desired function in windows? as if I skip this step it won't work at all
You don't need to do that, as Windows has no executable bit. Instead of running
./manage.py whatever
just run
python manage.py whatever
making sure that python.exe is on your PATH.

Django first app: error during the creation of sql tables from my models

I'm new in Python and I would to start some tutorial using Django.
I would use PyCharm as IDE.
So, I tried to create my start Django project.
I use a Mac, Python 2.7.5 version, Django 1.7.5 version.
I follow this tutorial to create my first Django app.
When I try to crate SQL Tables (in Pycharm I use this command: Alt + R, sql command and the name of my app example) I have this error:
bash -cl "/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Applications/PyCharm.app/Contents/helpers/pycharm/django_manage.py sql FirstExample /Users/Gianluigi/PycharmProjects/mycompany"
CommandError: App 'FirstExample' has migrations. Only the sqlmigrate and sqlflush commands can be used when an app has migrations.
Process finished with exit code 1
You don't need to use sql command. Assuming you have your database information correctly set up in settings.py, your python manage.py ... will be able to do everything for you.
In this case you would do python manage.py makemigrations mycompany followed by python manage.py migrate.
Lets say we have an app users. Add that app to settings
models.py:
class User(Django_User):
observations = CharField(max_length=2048, null=True, blank=True)
you should run the following command first to create the migrations:
python manage.py makemigrations
This will create your database tables
That's it!
https://docs.djangoproject.com/en/1.7/topics/migrations/
https://www.jetbrains.com/pycharm/quickstart/django_guide.html
while following this link, in CREATING DATABASE section when it says use the magic Ctrl+Alt+R shortcut twice:
we actually use it once and in console type makemigrations and then migrate

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.

Cannot access database API in Django

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.

Categories

Resources