Unregistering ModelAdmin view and registering it again changing its contents is not realized when invoking it.
|Reported by:||heidar_rafn||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
When trying to make a change to the list_per_page option of a ModelAdmin class instance,
it seems that the change is not activated on the fly.
I first "unregister" the old ModelAdmin class and then "register" a new one (which is actually created)
but when trying to invoke the view, the old one is used, it looks like it is cached somewhere and the url-mechanism
is not using the re-created view, but invokes the old one.
After some investigation, I managed to create a work around that demonstrates what might be a bug,
see the code snippets, especially the "changelist_view" function - if it is omitted,
the behavior I am describing happens.
Here are code snippets that demonstrate this:
# settings.py ... FLIGHTADMININSTANCE = None ...
# myapp/admin.py ... from django.conf import settings from myapp.models import ConfigParameters ... class FlightAdmin(admin.ModelAdmin): try: list_per_page = int(ConfigParameters.objects.get(name="FLIGHTADMIN_LIST_PER_PAGE").value) except: list_per_page = 5; ... def __init__(self, *args, **kwargs): super(FlightAdmin, self).__init__(*args, **kwargs) # store globally the object instance pointer settings.FLIGHTADMININSTANCE = self # following debug shows that the list_per_page is really changed print "FlightAdmin created", self, self.list_per_page def changelist_view(self, request, extra_context=None, **kwargs): # NOTE: following is to override some probably cached instance of the view # and enables the changed view to be used instead of the first instance created # of the FlightAdmin class if this hook is not used return super(FlightAdmin, settings.FLIGHTADMININSTANCE).changelist_view(request, extra_context=extra_context) ... admin.site.register(Flight, FlightAdmin) ... def list_per_page_change(sender, instance, created, **kwargs): if instance.name == 'FLIGHTADMIN_LIST_PER_PAGE': # yes - the value is changing, change the FlightAdmin class accordingly admin.site.unregister(Flight) try: admin.site.register(Flight, FlightAdmin, list_per_page=int(instance.value)) except: admin.site.register(Flight, FlightAdmin) post_save.connect (list_per_page_change, sender=ConfigParameters)