This snippet is used to create a script for monitoring sphinx status with Nagios via [django-sphinx](http://code.google.com/p/django-sphinx/).
It returns 0 (OK) or 2 (CRITICAL).
Remember to change this strings `ModelToMonitor` and `app_name`.
Usage :
`./manage your-controls-command --log` > /your/script/name.py
This snippet is used to monitor sphinx status via [django-sphinx](http://code.google.com/p/django-sphinx/).
It returns 0 (OK) or 2 (CRITICAL).
Remember to change this strings `ModelToMonitor` and `app_name`.
Usage :
`./manage your-controls-command --log`
A simple view used to manage the page cache stored in the database (here Postgresql in the django_cache table, you also have to set correctly CACHE_TABLE_OID, by the OID of the cache table (you can get it in PgAdmin)
This management command discovers media files from all `INSTALLED_APPS` (or the apps you specify) and copies or links them to `MEDIA_ROOT`. Put this code in a file like so:
yourapp/management/commands/collectmedia.py
...and don't forget the necessary `__init__.py` files.
This command includes an interactive mode (`-i` or `--interactive`) and a dry run mode (`-n` or `--dry-run`) for previewing what will happen. See `manage.py help collectmedia` for more options.
"Make fixture" command. Highly useful for making test fixtures.
Use it to pick only few items from your data to serialize, restricted by primary keys.
By default command also serializes foreign keys and m2m relations.
You can turn off related items serialization with `--skip-related` option.
How to use:
python manage.py makefixture
will display what models are installed
python manage.py makefixture User[:3]
or
python manage.py makefixture auth.User[:3]
or
python manage.py makefixture django.contrib.auth.User[:3]
will serialize users with ids 1 and 2, with assigned groups, permissions and content types.
python manage.py makefixture YourModel[3] YourModel[6:10]
will serialize YourModel with key 3 and keys 6 to 9 inclusively.
Of course, you can serialize whole tables, and also different tables at once, and use options of dumpdata:
python manage.py makefixture --format=xml --indent=4 YourModel[3] AnotherModel auth.User[:5] auth.Group
I often write short test or development scripts that are intended as one-offs. I typically want to execute these scripts from the command line but am always forgetting the necessary steps to set up the Django environment [as described here][bennett].
This snippet allows you execute arbitrary Python scripts from the command line with the context of a given project:
python manage.py execfile /path/to/some/script.py
Add the code to a file named `execfile.py` within the `management/commands` directory of one of your apps ([see here][ref] for details).
[ref]: http://www.djangoproject.com/documentation/django-admin/#customized-actions "Customized Actions"
[bennett]: http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/ "Standalone Django Scripts"
`backupdb` command allows to make a database backup automatically. It's supposed to do this just before a `syncdb`, `reset` or `flush` command in a server deployment. A usual upgrade task in production server could be:
./manage.py backupdb
./manage.py reset myapp
./manage.py syncdb
Put this code in your project's `management/commands/backupdb.py` file.
This creates a fixture in the form of a python script.
Handles:
1. `ForeignKey` and `ManyToManyField`s (using python variables, not IDs)
2. Self-referencing `ForeignKey` (and M2M) fields
3. Sub-classed models
4. `ContentType` fields
5. Recursive references
6. `AutoField`s are excluded
7. Parent models are only included when no other child model links to it
There are a few benefits to this:
1. edit script to create 1,000s of generated entries using `for` loops, python modules etc.
2. little drama with model evolution: foreign keys handled naturally without IDs, new and removed columns are ignored
The [runscript command by poelzi](http://code.djangoproject.com/ticket/6243), complements this command very nicely! e.g.
$ ./manage.py dumpscript appname > scripts/testdata.py
$ ./manage.py reset appname
$ ./manage.py runscript testdata
Add fcgi to settings.INSTALLED_APPS then you can start and stop FCGI through manage.py
>python manage.py startfcgi
>python manage.py stopfcgi
In settings define runfcgi arguments using **FCGI_*** in settings
For example:
>FCGI_SOCKET='/var/tmp/project.sock'
>FCGI_PIDFILE='/var/run/project.pid'
One of **FCGI_SOCKET** or **FCGI_HOST**/**FCGI_PORT** will need to be defined, but if you forget they will error out.
**FCGI_PIDFILE** is required to be defined to allow the process to be terminated.
Uses jsmin or jspacker to compact javascript files (usage example in [this blog post](http://pedro.valelima.com/blog/2008/jan/17/deploying-compacted-javascript-django/))
This is useful especially during the model-creation stage, when things are in constant flux. The `freshdb` command will drop the project's database, then create a new one. A common use case:
manage.py freshdb
manage.py syncdb
! Note - no longer needed
Save this script in the same directory as manage.py and run it through the command line.
It picks up project Command class instances. Something that will hopefully be fixed in the Django SVN version soon.
Heres an example of a command:
#utils/management/commands/sqlallall.py
from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.db import models
class Command(BaseCommand):
help = "Returns sqlall for all installed apps."
def handle(self, *args, **options):
"""
Returns sqlall for all installed apps.
"""
for app in models.get_apps():
call_command("sqlall", app.__name__.split(".")[-2])
Allows for a quick startup loading commonly used classes, installed apps, and console utils.
To use: After manage.py shell, enter from somepath.startup import *.