Django newforms-admin AlreadyRegistered gotcha

While converting one of our internal django-based applications to the newforms-admin branch, we were snagged by a little gotcha. After following the migration docs, we fired up our app and immediately upon accessing it, were presented with AlreadyRegistered exceptions. These exceptions are thrown if you attempt to register the same model twice with the same admin site instance. We found this odd, given that we were quite sure that we only registered the code once, in exactly one place.

The culprit turned out to be our use of "from models import *" in our URLConf. The newforms-admin docs currently have all of the admin registration at the bottom of the models file. The idea of putting them there is to guarantee that the code gets executed at some point. The problem is guaranteeing that it gets executed exactly once. By django loading up the models file, the registration was executed once. By us calling the star import in our URLConf, the registration code was being called again, causing the exception.

In the aforementioned docs, the authors mention having a preferred location for the admin site configuration. We ended up heeding it and created an admin.py file to contain everything. This file is now star imported from our URLConf to ensure the registration does proceed. However, by splitting them, we prevented multiple registration and better decoupled the admin site configuration from our models.

Anyway, this is a little gotcha if you're moving to newforms-admin, which will likely be a requirement for the 1.0 release of django.

This entry was posted on Wed, 22 Aug 2007 23:53:16 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.


Trackbacks

Use the following link to trackback from your own site:
http://dev.servprise.com/weblog/trackbacks?article_id=django-newforms-admin-alreadyregistered-gotcha&day=22&month=08&year=2007

Comments

Leave a response

Leave a comment