August archive

Model syntax changed

August 25, 2005

By popular demand, I'm about to change Django's model syntax to use "fieldname = FieldClass" instead of "FieldClass('fieldname')". This has been a longstanding ticket.

This change is BACKWARDS-INCOMPATIBLE, so don't "svn update" your code until you've changed your models. Old model syntax will not work.

I apologize for the backwards-incompatibility, but this is still unofficial software. ;-) Once we reach 1.0 -- which is much closer now that the model syntax is changed -- we'll be very dedicated to backwards-compatibility.

I can't think of any other backwards-incompatible changes that we're planning before 1.0 (knock on wood). If this isn't the last one, though, it's at least the last major one.

Here's a quick example of the new syntax:

class Foo(meta.Model):
    first_name = meta.CharField("The person's first name", maxlength=30)
    last_name = meta.CharField(maxlength=30)
    bar = meta.ForeignKey(Bar)
    sites = meta.ManyToManyField(Sites)
    class META:
        ordering = ('-bar',)
        admin = meta.Admin(
            fields = (
                (None, {'fields': ('first_name', 'last_name', 'bar', 'sites')}),

    def __repr__(self):
        return self.first_name

Note that all non-field metadata goes in an inner class called "META".

Also note that ForeignKeys, ManyToManyFields and OneToOneFields are now required to have an explicit name (in the example, that's "bar" and "sites"). Before, field names for FK/MTM/OTO fields were optional (as a shortcut), and that caused people confusion -- they didn't know how to refer to fields within "admin.fields". Now it's explicit.

This change should be quite stable. I've been testing it for the past two days in our production setting at World Online. Thanks to that, I was able to iron out bugs as they came up. Also, I've updated every one of the model unit tests (and, hence, the model examples) to use the new syntax, and all tests pass.

Here's what you need to know to change your models:

Please send a note to the django-users mailing list or stop by IRC if you have any problems.

I've gone through the documentation and attempted to update it to use the new syntax. But I had to have missed something. If you're up for it, please take a look through our docs and let us know, by filing a ticket, if you find any bugs.

Finally, I just want to publicly thank mmarshall, rmunn, garthk, hugo, manuzhai and all the other great people who have contributed patches, ideas and passionate discussion to this change. This is the first really big community-driven improvement to Django, and it's been awesome to see it come to fruition. Thanks, guys!

Recent developments

August 17, 2005

In case you don't follow the Django timeline, here's a rundown of improvements we've made recently:

  • We've added documentation on how to integrate Django with a legacy database and the django-admin.py utility.
  • We've added a model validator, which catches common errors in model syntax. Just run "django-admin.py validate". The development server automatically validates all installed applications when you start it and each time you make code changes while it's running. If you can think of any more errors the validator should catch, let us know by filing a ticket.
  • Thanks to our great community, we've made dozens of small bug fixes, usability improvements and other enhancements in code and documentation. It just keeps getting better!

Oh, and if you can make it to London Sept. 5, head over to Sam Newman's meetup for Django developers.

Anonymous-session support

August 16, 2005

By popular demand, we've added support for anonymous sessions. See the full session documentation.

Here's a 10-second example. request.session is a simple, dictionary-like object:

def post_comment(request, new_comment):
    if request.session.get('has_commented', False):
        return HttpResponse("You've already commented.")
    c = comments.Comment(comment=new_comment)
    request.session['has_commented'] = True
    return HttpResponse('Thanks for your comment!')

IMPORTANT: If you're using a Django installation from before revision 469, you'll have to take a few small steps to use the new session system -- and the admin system uses the session system, so chances are you'll be affected by this change. For full details, see the "Added support for anonymous sessions" section of the backwards-incompatible changes page.

Fourth tutorial is up

August 8, 2005

The round of official tutorials continues, with 4 focusing on simple form processing and generic views.

Read it, and let us know in the comments how it can be improved. Of course, more is on the way.

Django community

August 6, 2005

By far the coolest part of releasing Django has been the formation of the community. Although Django has only been available for about a month, there's already a number of people writing about Django. To prevent all this great knowledge from being lost in the noise of the Internet, I've added an aggregator of Django information on our community page.

If you write about Django and you'd like to get your weblog added to the aggregator, just drop me a note and I'd be happy to add you.

The aggregator is, of course, written in Django itself (with the aid of Mark Pilgrim's Universal Feed Parser).

Update: There's now an RSS feed of the community page.