How I Moved My Commercial Projects to Newforms-Admin

Posted by Ross Poulton on Wed 23 July 2008 #django #programming #newforms

My projects were all running on an SVN checkout from late April 2007, after the Queryset-refactor branch was merged into trunk. This meant that I had to make a number of changes, on a public server, to incorporate modifications to file uploads, generic create/update views, and more.

In this post I'm only going to cover how I did the change to Newforms Admin, as the other changes were relatively simple for my projects.

I made these changes on a live server, whilst my projects were running. For the volume of changes I had to make, this was very straightforward. I use FastCGI, and because the FastCGI processes were already running I was able to modify the Python code without it taking effect until I restarted FastCGI.

As always, make sure you've got a backup that's easy to roll back to.

The first thing I did was create my admin.py files in each application. For example, for DjangoSites I removed this admin definition from my 'Website' model:

    class Admin:
        list_filter = ('verified', 'screenshot',)
        list_display = ('url', 'title', 'owner', 'created', 'verified', )
        search_fields = ('title',)

and moved it into websites/admin.py:

from django.contrib import admin
from djangosites.websites.models import Website

class WebsiteAdmin(admin.ModelAdmin):
    list_filter = ('verified', 'screenshot',)
    list_display = ('url', 'title', 'owner', 'created', 'verified', )
    search_fields = ('title',)

admin.site.register(Website, WebsiteAdmin)

It's useful to note that at this stage, if I restarted my Django FastCGI instances, nothing would be broken - but the Admin wouldn't work.

Next, my urls.py file had to be updated. Out with the old:

(r'^admin/', include('django.contrib.admin.urls')),

and in with the new:

from django.contrib import admin
admin.autodiscover()
...
urlpatterns = patterns('',
    (r'^admin/(.*)', admin.site.root),
)

The last step to do before restarting FastCGI was to update Django. All i did was CD into my Django trunk folder and run svn up.

Finally, I restarted FastCGI and browsed to /admin/.

This is very straightforward, as most of my models have basic Admin requirements. For applications where you don't need any fancy Admin functionality at all, your admin.py can be even simpler:

from django.contrib import admin
from mysite.models import FirstModel, SecondModel, ThirdModel

for model in [FirstModel, SecondModel, ThirdModel]:
    admin.site.register(model)

The only real gotcha with doing an svn up is that there have been a heap of backwards-incompatible changes made recently, so you really need to make sure you work through the list and make the required changes. The main one that tripped me up was on WhisperGifts where I still used oldforms in a few places. After moving these to newforms to match the rest of the site, it all worked a charm.

I recommend updating Django incrementally - eg update to 7476, make changes required for queryset-refactor, update to 7814, make changes required for file uploads, etc. This makes it easier to manage than one huge update.

Luckily as we approach Django 1.0 backwards-incompatible changes shouldn't be as regular, and most people should be able to stick to the official 1.0 release. This will be a godsend for those distributing applications, as they will be able to recommend a particular Django release and know it is stable and it works correctly.

Congratulations to everybody on the Django team who contributed to newforms-admin going live. I've only covered a raw conversion from the old admin to the new here, and haven't even looked at the new features that are available like application-specific admin screens and admin-level managers (to filter data at the admin level). This is a fantastic change to Django and everybody involved has put in a mammoth effort. Kudos to you all.