Archives

Categories

Django MultiRangeField and MultiRangeFormField

I’m building a website, and needed a way to save a list of pages and page ranges from a book or magazine. I wanted to to be a little more strict than just saving it as a string without any cleanup or validation, so I wrote a custom Django model field (and accompanying form field).

It either just validates and cleans so that

becomes the tidy

…or, if you comment out the raising of the ValidationError in the form field’s validate() method, it will actually clean up any junk characters for you, so even this horrible mess:

gets cleaned to

Enjoy!

If you have comments, please share them with me on Twitter or Facebook.

You should check out my podcast about movie magic and VFX: The Optical.