In Django's shell, how can I load ipython magic functions automatically? - django

I want to make the following ipython commands permanent when using django shell:
%load_ext autoreload
%autoreload 2
Unfortunately, Django doesn't seem to use my global ipython config, so putting them in my default_profile doesn't seem to work. Is there any way to have these executed automatically when running django shell?

You can use the django extentions package, which contains a shell_plus command. This command autoloads the models, but you also can use the --notebook attribute. There you can add the autoload parameter: --ext django_extensions.management.notebook_extension.
See here for more info.

Related

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.

Is there a way to add custom django-admin.py commands that work outside of projects?

I'm trying to write a custom command that works outside of Django projects. I was thinking I could follow the coding patterns of Django's own such commands (e.g., startproject), include my command in an app and install it.
Alas, it seems django cannot see this command, as perhaps it doesn't scan site-packages for custom commands.
Is there a way to make this work or am I sadly correct?
UPDATE: I should note that the goal I was trying to accomplish (writing a command that starts projects based on custom templates) is supported in the coming 1.4 release of Django: https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-startproject (see the --template option).
Based on this code from django.core.management, it does appear that django only searches for project-less commands in its own packages, and will then only find command by scanning INSTALLED_APPS, which means a project is required.
You can use a custom manage.py.
You do need a project. A project is, although, nothing more than a python package with a settings.py (and maybe a urls.py file)
So you could just create a project, with whatever commands you want, and in your setup script include a binary script that is nothing more than a manage.py in disguise.
I use it to have a manage.py in the bin path of a virtualenv, but you can call it something else and have that "django" project installed in your system python.
I don't quite understand from your post, for what purpose do You want to write such command using Django's manage.py. But suppose you want (as I was) to run some script, that works with Django models, for example. You cannot run such script without setting Django environment.
I do the following:
put my code in script.py
manage.py shell
execfile('script.py')
Maybe, this helps.

Django, error with custom admin commands when executing with absolute path

I have a custom admin command named temperature.py which is under /home/user/project/monitor/management/commands. If I change directory to /home/user/ and execute:
user#localhost:~/project$ ./manage.py temperature
It runs ok, and its listed in the available commands. But if I try running it with the absolute path:
user#localhost:/$ /home/user/project/manage.py temperature
It says the command does not exist, it does not show up in the available commands either. I have django 1.2.1 with python 2.6.5 in ubuntu 10.04. Could this be a problem with django? is it the python version? Thanks in advance
Found the reason, it seems that django is looking for the settings under the main directory, if it fails to find one, it will use the defaults. You can change your python path or use this in your manage.py file

What is the correct configuration for %autoreload in a Django ipython shell?

Ipython has a plugin called autoreload that will presumably reload all your modules after every command, so you can change the source and not have to quit the shell and reenter all your commands. See http://dsnra.jpl.nasa.gov/software/Python/tips-ipython.html for example.
However, this seems flaky at best when using it with Django, e.g.
python manage.py shell
gives me an IPython shell with Django context, but the autoreloading does NOT seem to work reliably at all.
Here's what I have added to my ipy_user_conf.py file:
def main():
... # rest of the fn here
import ipy_autoreload
ip.magic('%autoreload 2')
The autoreloading works in limited cases, maybe 10-20% of the time.
Has anyone successfully configured this to work with Django?
This answer might also be applicable to your situation. Django keeps its own cache of all models, so if you want to reload everything, you have to clean this cache manually.