Living on the Edge

A Slug is a Slug

Posted on August 18, 2007

One other notable commit this past week is Changeset 5877. This particular changeset helps dry up generic views a bit. The modification sets the default slug_field key to 'slug'. Why is this important? Consider how we’ve been doing standard generic date-based views:


blog_dict = {
    'queryset': Article.objects.all(),
    'date_field': 'pub_date',
}

urlpatterns = patterns('django.views.generic.date_based',
    (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[\w-]+)/$', 'object_detail', dict(blog_dict, slug_field='slug')),
    (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$', 'archive_day', blog_dict),
    (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'archive_month', blog_dict),
    (r'^(?P<year>\d{4})/$', 'archive_year', blog_dict),
    (r'^/?$', 'archive_index', blog_dict),
)

Note that whenever we’re dealing with a slug field and the object_detail, we need to be sure to modify the dict to include the slug_field key. With Changeset 5877, the default is to set slug_field='slug'. Now we can just specify our URLConf like so:


blog_dict = {
    'queryset': Article.objects.all(),
    'date_field': 'pub_date',
}

urlpatterns = patterns('django.views.generic.date_based',
    (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[\w-]+)/$', 'object_detail', blog_dict),
    (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$', 'archive_day', blog_dict),
    (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'archive_month', blog_dict),
    (r'^(?P<year>\d{4})/$', 'archive_year', blog_dict),
    (r'^/?$', 'archive_index', blog_dict),
)

Much cleaner.

Registering Custom Commands with Manage.py

Posted on August 18, 2007

I think someone is reading my mail, or should I say mind. For the past several days I had been drafting a preliminary patch and rationale for proposing that the manage.py system be opened up to allow extensibility of custom commands. My initial motivation for this functionality was due to my Newforms Generator. I wanted to be able to inject this behavior into manage.py without having to submit a patch and get buy-in. Well thankfully it looks like someone else had the same exact idea. With Changeset 5925 we get this functionality.

Coming from Ruby / Rails world I have really missed Rake and the simple extensibility of that tool to automate common tasks. Now with extensibility in manage.py I can do a lot of the same things I was accustomed to previously.

The docs online are not updated yet to reflect this change but the Changeset patch has all the information you need to get started.

Very cool stuff.

UPDATE

As noted in the comments, it looks like this one was rolled backed for a bit. Something about it not working. :) I’m sure the required fixes will be put in place shortly.