Default (initial) value in Django's admin for a choice field

I was trying to set the initial value for a choice field in the admin like this:

from django import forms
from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from author.models import Author
from serials.constants import STATUS_CHOICES

class AuthorAdminForm(forms.ModelForm):
    class Meta:
        model = Author

class AuthorAdmin(MPTTModelAdmin):
    form = AuthorAdminForm(initial={'status': STATUS_CHOICES[0][0]})
admin.site.register(Author, AuthorAdmin)

Logically, I should have been able to do it this way, I thought, at least according to the docs. But instead it was giving me this error: TypeError: issubclass() arg 1 must be a class. (Uh, ok.) After a bit of searching, I eventually found this comment, and realized I must be trying to instantiate a form that the admin would instantiate automatically, had already been instantiated, or something like that.

After some more searching, I found the answer on the way to do what I needed here:

from django import forms
from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from author.models import Author
from serials.constants import STATUS_CHOICES

class AuthorAdminForm(forms.ModelForm):
    class Meta:
        model = Author

    def __init__(self, *args, **kwargs):
        super(AuthorAdminForm, self).__init__(*args, **kwargs)
        self.initial['status'] = STATUS_CHOICES[0][0]
            
class AuthorAdmin(MPTTModelAdmin):
    form = AuthorAdminForm
admin.site.register(Author, AuthorAdmin)

Yay! Hope this helps someone else who has the same issue. :)

Django trick: using named admin urls in templates

To add a link to an admin page on the user-side of your django site, you can use the named url patterns. (Seems like a simple and fairly straightforward thing to want to do, but I frequently find that things are not so simply found in most open source docs, nor are the examples always useful. But my usual open source docs rant is for another day. *g*)

It took a bit of digging, but I finally found that the admin url pattern options are listed here. From that, I was able to figure out that if I wanted to link to the edit (change action) page (parameter) for an article (model) in the app article, the pattern was:

{{ app_label }}_{{ model_name }}_action [parameters]

The namespace for the admin is, of course, admin, so putting that together using the {% url %} template tag, you get:

<a href="{% url 'admin:article_article_change' article.pk %}">{% trans 'Edit' %}</a>

That correctly creates the relative url to /admin/article/article/2. Yay!

Be sure to load the admin template tags in your template, too, or it won't work:

{% load admin_urls %}

Et voilà! *vbg* Hope this saves someone else a bit of time.

(This works for Django 1.4, btw.)

*howl*

Stephen Colbert interviewing Maurice Sendak:

ETA: The next part of the interview is here.

Holy Hannah, what are these authors thinking?

(Yes, I'm rewatching SG-1 lately. Hush.)

I'm checking out the websites of the authors attending COSine this weekend, so I have some idea of who they are, what they write, and how much they need my help, and wow, seriously. 1995-era GeoCities just called and wants its websites back. Yikes!

I mock because I love, but seriously, folks: the whole point of an author's site is to get readers to read what you've written, so give them that information. Most readers don't care about your cat, your past lives, or your obsession with Cheez-Its; we're visiting your site because we read something by you that we enjoyed and now we just want to read more of your stories.

More on this topic later. (I'm working on a presentation about it right now, as a matter of fact.)

 

I'm not just saying that: I really do love helping authors reach out to readers, and I hate seeing it done badly. It causes me physical pain every time I visit an author's site that looks like it was thrown together by the neighbor's kid or That Guy you know from work who knows web design. *rolls eyes* You get what you pay for, folks!

Htaccess trick

Here's what I ended up putting in an .htaccess file for each of the various sites I run/own/manage to participate in the SOPA/PIPA protest today:

RewriteEngine On
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0459
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1701
RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY} =20120118
RewriteRule ^(.*)$ http://sopastrike.com/strike/ [R=307,NC,L]

Learn something new every day. *g*

This forwarded each site to the protest page for a certain time frame (the length of the protest, adjusted for the server's time). You could use something like that to put a site in maintenance mode, for example. Just be sure to add a RewriteCond that checks for your IP so you can get to the site to test it. ;)