Skip to content

Admin change form doesn't preserve changelist filter #125

@sephii

Description

@sephii

When you save a polymorphic model form in the admin, you lose the changelist filter that was defined in your URL. This is because in django/contrib/admin/templatetags/admin_urls.py, Django compares the changelist url with the current one:

def add_preserved_filters(...):
    # ...
    current_url = '%s:%s' % (match.app_name, match.url_name)
    changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)

    if changelist_url == current_url and '_changelist_filters' in preserved_filters:
        # Get preserved filters

Since the changelist_url is constructed based on opts.model_name, you get the "implementation" model name, not the base one, which is the one from current_url. For example if you have a base model named "BaseTree" and an implementation named "OakTree", changelist_url would be admin:tree_oaktree_changelist but current_url would be admin:tree_basetree_changelist. So the 2 URLs don't match and the changelist filters are not correctly processed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions