Here are a couple options I think we should consider:
1. Remove the exception and allow subsequent registrations of
ModelAdmin classes to simply update the registry. This allows for user
code to override ModelAdmins in django.contrib or other third party
application. That could also be accomplished with just defining your
own site, but that path for many beginners may be more difficult unless
documented well.
2. Throw the exception based on whether the ModelAdmin is different
from a previously registered one. This will technically keep the
current behavior, but is really special casing the instances where
admin.site.register is called more than once. This currently happens
every so often with the models.py which is the documented method of
registration.
3. Keep current behavior and let documentation take care of it.
I personally am in favor of #1, but the only problem I have with it is
in the case of unintended side-effects that may result in much more
difficult debugging. What are everyone else's thoughts?
--
Brian Rosner
http://oebfare.com
This is a little unstable because it depends on the order of imports.
Once we get into a battle of "this must go ahead of this" in
INSTALLED_APPS, it's a pretty slippery slope. Plus, imports happen at
reasonably hard to predict times (the ordering can be different to what
you expect) in any case, due to the app_cache. So I'd try to avoid this
path.
> 2. Throw the exception based on whether the ModelAdmin is different
> from a previously registered one. This will technically keep the
> current behavior, but is really special casing the instances where
> admin.site.register is called more than once. This currently happens
> every so often with the models.py which is the documented method of
> registration.
This would seem like the obvious approach, providing there's a way to
compare two ModelAdmin subclasses for equality. It doesn't even have to
be a particularly cheap/fast test, since it doesn't happen that often.
>
> 3. Keep current behavior and let documentation take care of it.
People will be bitten by different import paths. It's far too easy to
have both "app" and "project.app" imported without realising why (or any
number of variations on this theme). We get bitten by this all over the
place in Django and it's to be avoided if at all possible.
Malcolm
--
Monday is an awful way to spend 1/7th of your life.
http://www.pointy-stick.com/blog/
I'm +0 on option 2, too, so really either one's OK.
Jacob
I wonder if that's mixing up two problems: accidental re-registration
and intentional overriding. A possible solution is that intentional
overriding passes in a parameter to the registration method to say that
it's expected/permitted to override anything that is already registered.
On the other hand, I don't think removing the error check is going to
hurt much either for most people. There's nowhere else in a language
like Python (or C or Perl or ...) where you have to pre-announce "I'm
going to be using your name now." and the originally scoped variable
gets to complain and veto.
Malcolm
--
If Barbie is so popular, why do you have to buy her friends?
http://www.pointy-stick.com/blog/
I've hit this error a few times myself, but I haven't had a chance to
dig into the code to find the exact cause and the right solution.
However, my preliminary investigations suggested that this was a
variant on the multiple registration problems we used to have before
the model cache was refactored. If this is the case....
> Here are a couple options I think we should consider:
>
> 1. Remove the exception and allow subsequent registrations of
> ModelAdmin classes to simply update the registry. This allows for user
> code to override ModelAdmins in django.contrib or other third party
> application. That could also be accomplished with just defining your
> own site, but that path for many beginners may be more difficult unless
> documented well.
This sounds like a recipe for disaster to me. The end result - being
able to extend a ModelAdmin definition - is potentially useful, but
the unpredictability of registration order means that we won't really
be able to easily control (or document) whether ModelAdmin A extends
ModelAdmin B, or vice versa.
> 2. Throw the exception based on whether the ModelAdmin is different
> from a previously registered one. This will technically keep the
> current behavior, but is really special casing the instances where
> admin.site.register is called more than once. This currently happens
> every so often with the models.py which is the documented method of
> registration.
+1 to this option. We need to prevent users from registering a model
with an admin site multiple times, but it is possible for a single
site.register to get invoked multiple times as a result of the import
process. Checking if the registration has changed seems like a good
way to achieve this, and is somewhat analogous to the way the model
cache extracts the app.model name from a model definition and using
that as the basis for eliminating duplicates.
> 3. Keep current behavior and let documentation take care of it.
If I have correctly understood the errors I was getting, this isn't
really something that documentation can (or should) fix, because there
are perfectly valid Python import statements that will cause
difficulty for the admin registration process.
Yours,
Russ Magee %-)
Regards
Rajeev J Sebastian
I guess my very terse response caused some misunderstanding.
I meant, we (i.e., dinamis.com in our inhouse projects) put all our
admin related stuff for each app in an <app>/admin.py and load it at
startup.
Sorry for the misunderstanding I caused.
Regards
Rajeev J Sebastian