1. Skip to navigation
  2. Skip to content

Entries tagged “commands”

Django Command Extensions Update

written by Michael Trier, on May 29, 2008 10:57:00 AM.

Since I published my introductory post on the project, I have not posted any additional updates regarding the project. Quietly, there’s been tons of work happening, and so I thought I would give some visibility to all of the great contributions to the effort.

As a review, the django-command-extensions project is a repository for collecting global custom management extensions for the Django Framework.

Since the project was started there has been an additional twelve commands added. The bulk of the work has been headed up by Ido Sebastiaan van Oostveen, with some additional work by Doug Napoleone and Ludvig Ericson. Personally, I haven’t had much involvement in the development beyond a few initial commands. Although, I have been a satisfied customer of the extensions, such that it has become a staple for all of my Django projects.

So on to the fun stuff. Here’s a list of commands in the project:

  • create_app – creates an application directory structure for the specified app name. This command allows you to specify the --template option where you can indicate a template directory structure to use as your default.
  • create_command – creates a command extension directory structure within the specified application. This makes it easy to get started with adding a command extension to your application.
  • create_jobs – Creates a Django jobs command directory structure for the given app name in the current directory. This is part of the impressive jobs system.
  • create_superuser – makes it easy to create a superuser for the django.contrib.auth.
  • describe_form – used to display a form definition for a model. Copy and paste the contents into your forms.py and you’re ready to go.
  • – export the email addresses for your users in one of many formats. Currently supports Address, Google, Outlook, LinkedIn, and VCard formats. I have found this really handy.
  • generate_secret_key – creates a new secret key that you can put in your settings.py module.
  • – creates a GraphViz dot file. You need to send this output to a file yourself. Great for graphing your models. Pass multiple application names to combine all the models into a single dot file. This one is very useful with lots of options for flexibility. See the wiki page for detailed information.
  • passwd – makes it easy to reset a user’s password
  • run_job – run a single maintenance job. Part of the jobs system.
  • run_jobs – runs scheduled maintenance jobs. Specify hourly, daily, weekly, monthly. Part of the jobs system.
  • runprofileserver – starts runserver with hotshot/profiling tools enabled. I haven’t had a chance to check this one out, but it looks really cool.
  • shell_plus – An enhanced version of the Django shell. It will autoload all your models making it easy to work with the ORM right away. I use this every day. It is so handy.
  • show_urls – displays the url routes that are defined in your project. Very crude at this point.
  • sqldiff – prints the (approximated) difference between an apps models and what is in the database. This is very nice, but also very experimental at the moment. It can not catch everything but it’s a great sanity check.

On the documentation front, Ido has been actively putting together some and usage instructions to help people get started. We still have quite a few undocumented commands so if you would like to pitch in, we appreciate your help.

If you like using Mercurial, Ido maintains a Mercurial repository for the project. You can find more information on using his repository on the .

I would like to thank Ido Sebastiaan van Oostveen for his help. He’s really taken a leadership role and contributed a lot of great stuff. Additionally he’s been fleshing out the documentation and managing the tickets / patches.

Finally, if you would like to get involved in the project, we’re always looking for people to help out. Feel free to . If you would like to contribute directly, please let me know.

Management Command Extensions Project

written by Michael Trier, on Nov 22, 2007 11:36:00 PM.

If you have global custom management command extensions or if you have suggestions for some that would be useful to you in your daily work with Django, . Yesterday I posted a new project on , called , that will hopefully grow into a nice repository of custom management command extensions. Currently I’ve included the following three commands:

  • describe_form – Writes out a newforms definition to the console for the specified model
  • create_command – Generates a custom management command directory structure in the application specified, which makes it easy to get started creating your own commands.
  • generate_secret_key – Generates and displays a new secret key that can be used in your settings.py module.

There’s no documentation at the moment but that will come shortly. The application should be installed on your python path like all Django applications. Your Django framework must be at least updated to Changeset 6047.

Join In

Here’s a couple ways you can help out:

  • Become a Committer – If you’d like to contribute to the code please let me know and I’ll add you.
  • Test – Use the commands on various operating systems, different environments. .
  • Make Suggestions – If you have ideas about how we can expand the functionality of current command extensions or if you have a suggestion for a new command extension, .
  • Documentation – Like to write documentation? Okay, but would you be willing to? Let me know.

If you need help getting started in creating your own custom management commands please check out the screencast I did on the subject.

The code is licensed under the MIT license.

Regenerate Secret Key

written by Michael Trier, on Nov 21, 2007 7:43:00 PM.

I had a need to generate a new secret key for a Django project that I’m working on. Often when I start a new project I’ll just copy an existing project template that I have which has all the bits and pieces in the right places. This helps me to get going quickly without a lot of fuss. Although when doing so I always need a new SECRET_KEY for the settings file. Instead of doing it manually this time, I decided to create a new custom management command and make it part of my global . For doing this I just inherited from the NoArgsCommand and extracted the logic for creating the secret key from the startproject command in the Django source.


from random import choice
from django.core.management.base import NoArgsCommand

class Command(NoArgsCommand):
    help = "Generates a new SECRET_KEY that can be used in a project settings file." 

    requires_model_validation = False
    can_import_settings = True

    def handle_noargs(self, **options):
        return ''.join([choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)])

Using call_command

written by Michael Trier, on Nov 21, 2007 12:54:00 AM.

While creating the most recent Django Screencast on Custom Management Commands, I had it in my show notes to mention how you can call Django’s management commands from an external script. There’s probably several different ways to accomplish this, but the simplest and “baked in” way to do this is through the use of the call_command function. This function is part of django.core.management.

For example, if we wanted to call sqlall from the shell we could do something like the following:


>>> from django.core.management import call_command
>>> call_command('sqlall', 'delinkuent')

The call_command function’s signature looks like:


call_command(name, *args, **options)

It accepts the name of the command we’re executing, any required arguments, and options to the command. If we have a command with an argument and options it might look like the following:


>>> call_command('say_hello_name', 'michael', capitalize=True)
Hello Michael

This approach works fine with any custom management commands you may have created as well.

There is a caveat to this approach. You must be sure that you’ve properly set up your DJANGO_SETTINGS_MODULE environment variable. For more information about this see James Bennett’s excellent writeup on the subject.

Custom Management Commands Cheatsheet

written by Michael Trier, on Nov 21, 2007 12:14:00 AM.

I mentioned in the Django Screencast that I would be putting together a cheatsheet on Django’s custom management commands, so here it is. I hope you find it useful.

Django Screencasts - Episode 003

written by Michael Trier, on Nov 20, 2007 1:49:00 AM.

In this screencast I cover adding custom management commands into your Django applications. Custom management commands are a method of extending Django’s built-in commands that are made available through the manage.py or django-admin.py scripts.

Source code for the screencast is provided below. Please be sure to read the README file included in the download.

Downloads

Regular (64.2 MB, 41:09, H.264, MP4)

IPod (26.6MB, 41:09, H.264, MP4)

Source Code (124 KB)

Requires the latest version of QuickTime, VLC Media Player, or comparable software capable of playing MP4 container format and the H.264 codec.

Show Notes

  • Django Documentation on Custom Management Commands
  • Registering Custom Commands with Manage.py

P.S. I apologize for the delay in getting this screencast put together. Between Leopard upgrades and health issues time got away from me.

Update

My server was getting hammered with downloads and was unavailable for a while this morning. I’ve moved the downloads to S3 to try to offload some of the impact. Hopefully this will correct the issue. If you experience problems downloading the screencast, please email me.