Some django stuff

I generally love Django, but it does have some real holes. The worst problem for me is its complete lack of support for database changes. The Django devolppers argue, maybe convincingly, that database evolution is delicate enough that it’s dangerous to automate. But I feel like it’s dangerous enough it shouldn’t all be left to me either. I’m not particularly scared of SQL per se, but I know I could easily get something wrong. Do Rails’ schema evolution things make that easier, or at least less scary?

I just added “created” and “modified” date fields to one of my models, both using Django’s automated date fields, that set themselves to the current date when an object is added/modified. That led to an extra complication: I wanted the real dates for all my already-existing data. Fortunately Django’s admin keeps a log from which all the modification dates can be extracted. Once gotten, they can’t be saved to the database through Django’s normal model inerface–as that would be a modification that would auto-set the modification date, exactly what I don’t want to do.  So that required a bit more SQL.

OK, despite my whingeing the SQL for that was pretty trivial.  I’ll suck it up now. The interesting bit was the python function for extracting dates from logs:


def getdate(obj, addition):
    Look at the logs to find creation/modfication dates.
    model = type(obj)
    # we need the contenttype
    ct = ContentType.objects.get_for_model(model)
    # now we can look up the logs for this object
    logs = LogEntry.objects.filter(content_type=ct,
    dates = [ for l in logs \
                if not addition or l.is_addition() ]
        return dates[0]
    except IndexError:
        return None

Tags: ,

3 Responses to “Some django stuff”

  1. Simon Willison Says:

    Have you looked at ? It might be the solution you’re looking for.

  2. mrlauer Says:

    Thanks, Simon–I’m using Django 0.96, but if/when I get around to upgrading it might well be just what I want.

  3. Upgrading django « Michael Lauer’s Weblog Says:

    […] of django.  It has a bunch of new little features I’d like to use; and the next time I change the database I want to try django-evolution, which doesn’t work on […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: