new-forms admin shouldn't throw error when a model is registered twice #6776

114 views
Skip to first unread message

Simon Willison

unread,
Apr 11, 2008, 5:56:04 AM4/11/08
to Django developers
http://code.djangoproject.com/ticket/6776

newforms-admin raises an AlreadyRegistered exception if you attempt to
register a model with admin.site twice. Unfortunately, due to the
different places and ways in which a models.py file can be imported
it's extremely easy to trigger this accidentally, and difficult to
debug when you do.

The code in question is here:

http://code.djangoproject.com/browser/django/branches/newforms-admin/django/contrib/admin/sites.py#L78

if model in self._registry:
raise AlreadyRegistered(
'The model %s is already registered' % model.__name__
)
self._registry[model] = admin_class(model, self)

Is there any reason we can't just ignore attempts at repeat
registrations here?

Cheers,

Simon

mrts

unread,
Apr 11, 2008, 6:47:54 AM4/11/08
to Django developers
As double imports should be considered harmful, I'd rather see a
better warning there, in the lines of ""The model Foo is already
registered. This can be caused by a double import, see docs/bar.txt
for ways to avoid it." (Double imports are triggered by mixing
relative and absolute imports, e.g. in "import models" in foo/views.py
and "from project.foo import models" in bar/views.py.)

There should perhaps be a section "How to organize your project and
applications" in main documentation or in the FAQ that would give
clear guidelines for creating reusable applications yet avoiding
double imports.

On Apr 11, 12:56 pm, Simon Willison <si...@simonwillison.net> wrote:
> http://code.djangoproject.com/ticket/6776
>
> newforms-admin raises an AlreadyRegistered exception if you attempt to
> register a model with admin.site twice. Unfortunately, due to the
> different places and ways in which a models.py file can be imported
> it's extremely easy to trigger this accidentally, and difficult to
> debug when you do.
>
> The code in question is here:
>
> http://code.djangoproject.com/browser/django/branches/newforms-admin/...

NickFitz

unread,
Apr 11, 2008, 7:24:19 AM4/11/08
to Django developers
On Apr 11, 11:47 am, mrts <m...@mrts.pri.ee> wrote:
> As double imports should be considered harmful, I'd rather see a
> better warning there, in the lines of ""The model Foo is already
> registered. This can be caused by a double import, see docs/bar.txt
> for ways to avoid it."

Just wondering: why exactly should double imports be "considered
harmful"? If the framework detects them and silently ensures that Bad
Things Don't Happen, then they aren't harmful, and it's one less
finicky little detail to bog one down hunting all over for the source
of the problem.

Russell Keith-Magee

unread,
Apr 11, 2008, 7:36:47 AM4/11/08
to django-d...@googlegroups.com
On Fri, Apr 11, 2008 at 5:56 PM, Simon Willison <si...@simonwillison.net> wrote:
>
> http://code.djangoproject.com/ticket/6776
>
> newforms-admin raises an AlreadyRegistered exception if you attempt to
> register a model with admin.site twice. Unfortunately, due to the
> different places and ways in which a models.py file can be imported
> it's extremely easy to trigger this accidentally, and difficult to
> debug when you do.

Brian raised this issue about a month ago, albeit from a slightly
larger perspective:

http://groups.google.com/group/django-developers/browse_thread/thread/1a40c64e9c952c48

The discussion there may be slightly more illuminating regarding the
reasoning and options.

Russ %-)

Reply all
Reply to author
Forward
0 new messages