Living on the Edge

Django Command Extensions Update

Posted on May 29, 2008

Since I published my introductory post on the django-command-extensions 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_emails – 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.
  • GraphModels – 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 installation 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 wiki page.

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 submit patches or bug / enhancement reports. If you would like to contribute directly, please let me know.

Management Command Extensions Project

Posted on November 22, 2007

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, we want you. Yesterday I posted a new project on Google Code, called Django Command Extensions, 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. Give us feedback.
  • 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, let us know.
  • 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

Posted on November 21, 2007

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 management command extensions project. 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)])