Login

Tag "commands"

Snippet List

Management command which helps to find temlate files

If you need to customize many default templates from installed apps, this management command will help you to find those templates and to copy them to desired location. Place this code at: management/commands/templates.py To see a list of installed templates, run: python manage.py templates To copy all templates to specified location: python manage.py templates --copy-to ./templates To copy templates from specified applications only: python manage.py templates admin auth --copy-to ./templates

  • management
  • commands
  • command
Read More

Continuous Integration command

This command, `runtester` will run the test suite whenever files are modified. It takes the apps to test as arguments; if no apps are given the entire test suite is run. Use this command just as `runserver` is used; fire it up in a shell and it does its thing. Copy this snippet into `django/core/management/commands/runtester.py`.

  • testing
  • tests
  • commands
  • command
  • test-runner
Read More

Custom management command to list recent admin actions

On a busy site it can be nice to have a summary of admin activity. Running this command (I call it "adminlog") generates output like this: 2009-07-10 18:06:19: pbx changed flat page: "/yay/ -- Let's All Say Yay" By default it shows the last five actions; pass it a numerical arg to show more or fewer. Run this as a cron job and you can follow a site's admin-side activity without even logging in!

  • admin
  • commands
Read More
Author: pbx
  • 1
  • 3

Management command to list custom management commands

I work with multiple projects, many of which have multiple custom management commands defined. It can be hard to remember them, and slow to pick them out of the "manage.py help" list. This quickie command lists all of a project's custom commands (along with their help text). Writing it was easy after looking at the source of django.core.management. Open questions include: how do you decide which app to put this command in? Should this command list itself?

  • management
  • commands
Read More
Author: pbx
  • 5
  • 8

syncdata command

A django admin command that takes a fixture and makes the target database the same as that fixture, deleting objects that in the database but not in the fixture, updating objects that are different in the database, and inserting missing ones. Place this code in your_app/management/commands/syncdata.py You will need to use manage.py (not django-admin.py) for Django to recognise custom commands (see http://www.djangoproject.com/documentation/django-admin/#customized-actions). This snippet is the 'loaddata' command with this patch applied: http://code.djangoproject.com/ticket/7159 (with minor tweaks). The intention is that 'dumpdata' on system A followed by 'syncdata' on system B is equivalent to a database copy from A to B. The database structure in A and B must match.

  • admin
  • database
  • import
  • commands
  • copy
  • command
  • synchronise
  • publish
Read More

Command to make fixtures.

"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

  • serialize
  • admin
  • model
  • fixtures
  • tests
  • test
  • management
  • commands
  • fixture
  • command
  • make
Read More

Command to dump data as a python script

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

  • dump
  • manage.py
  • serialization
  • fixtures
  • migration
  • data
  • schema-evolution
  • management
  • commands
  • command
Read More

extras.py for management commands

! 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])

  • management
  • commands
Read More

9 snippets posted so far.