Thursday, April 4, 2013

Django ListFilter e=1

I ran into some odd issue with Django the other day when attempting to inherit the ListFilter class in the Django Admin app.

I was creating a custom filter to allow a date range to be selected; and doing this required that I add the date parameters into the query string. Not a big deal, I thought. Well, turns out; it was a bigger deal than I thought. Apparently Django Admin had decided that query string parameters needed to be explicitly declared by the filters; otherwise the app would silently error on them; and replace them with e=1 (strange choice, I think). I wandered all over the internets to find an answer to why my query string class_start_time=2013-01-01 was being converted to e=1. Nothing. Crickets.

I ended up debugging the app itself to determine the root cause of the problem; Pycharm made this a fairly painless procedure.

All of that to say: If you ever find yourself wanting to inherit the ListFilter; and adding some of your own query string values; you need to remove those from the params dictionary that will get passed to your classes __init__ method. 

del params[parameter_name]

Most of the time, the Admin app makes life very easy, and sometimes you end up spending an afternoon debugging source files to figure out what's going on. At least there are well written source files to debug!

Here's a link to the StackOverflow question I posted on this: http://stackoverflow.com/questions/15792884/creating-custom-list-filter-in-django/15797139#15797139


No comments: