tags:

views:

845

answers:

4

What I would like to achive is:

  • I go to admin site, apply some filters to the list of objects
  • I click and object edit, edit, edit, hit 'Save'
  • Site takes me to the list of objects... unfiltered. I'd like to have the filter from step 1 remembered and applied.

Is there an easy way to do it?

+1  A: 

Click 2 times "Back"?

Alex Koshelev
KISS at its best :)
Tomasz Zielinski
+2  A: 

Unfortunately there's no easy way to do this. The filtering does not seem to be saved in any session variable.

Clicking back twice is the normal method, but it can be unweildy and annoying if you've just changed an object so that it should no longer be shown using your filter.

If it's just a one-off, click back twice or go through the filtering again, it's the easiest way.

If you're going to be filtering more often, or you just want to learn about hacking the admin (which is pretty open and easy), you'll want to write a FilterSpec.

Have a look here and here for examples of people writing their own.

A really, really terrible way to do this would be to edit the admin interface so that after you click "Save", you are redirected to you filtered URL. I wouldn't recommend this at all, but it's an option.

Another fairly simple way to do this would be to write a generic view to show your filtered objects, then use Django forms to edit the items from there. I'd have a look at this, you'll be stunned just how little code you have to write to get a simple view/edit page going.

Harley
Writing a `FilterSpec` isn't enough. It's actually the view which controls the queryset, and that takes its parameters from `request.GET`. All `FilterSpec`s really do is affect the display of filter options.
Ben James
Also, redirecting to a filtered URL is not "really, really terrible". The filter options should be explicit in the URL. It would be better than having a invisible session variable affect the queryset without this being evident in the URL.
Ben James
+1  A: 

There's a simple hack to do this, but it's not a general solution and requires modifying every ModelAdmin which you want to support this. Maybe there is a general way to do this, but I've not spent the time to solve it on a general level.

The first step is to write a custom FilterSpec for the filter (see Harley's post for links that will help) which saves the chosen filter value in the session (and deletes it when no longer wanted).

# in cust_admin/filterspecs.py
from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec

class MyFilterSpec(ChoicesFilterSpec):

    def __init__(self, f, request, params, model, model_admin):
        super(MyFilterSpec, self).__init__(f, request, params, model,
                                           model_admin)
        if self.lookup_val is not None:
            request.session[self.lookup_kwarg] = self.lookup_val
        elif self.lookup_kwarg in request.session:
            del(request.session[self.lookup_kwarg])

# Register the filter with a test function which will apply it to any field
# with a my_filter attribute equal to True
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'my_filter', False),
                               MyFilterSpec))

You must import the module this is in somewhere, for example your urls.py:

# in urls.py
from cust_admin import filterspecs

Set a property on the field you want to apply the filter to:

# in models.py
class MyModel(models.Model):
    my_field = Models.IntegerField(choices=MY_CHOICES)
    my_field.my_filter = True

In a custom ModelAdmin class, override the change_view method, so that after the user clicks save, they are returned to the list view with their filter field value added to the URL.

class MyModelAdmin(admin.ModelAdmin):
    def change_view(self, request, object_id, extra_context=None):
        result = super(MyModelAdmin, self).change_view(request, object_id,
                                                       extra_context)
        if '_save' in request.POST:
            if 'my_field__exact' in request.session:
                result['Location'] = '/admin/myapp/mymodel/?my_field__exact=%s' \
                                     % request.session['my_field__exact']
        return result
Ben James
Does this mean that every time you view the model's admin page it will be filtered? Or does it just remember the filter you've applied for a session?
Harley
A: 

Another way to do this is to embed the filter in the queryset.

You can dynamically create a proxy model with a manager that filters the way you want, then call admin.site.register() to create a new model admin. All the links would then be relative to this view.

shaunc