Unit testing abstract models in Django and factoryboy

I had to test an abstract model recently and I also wanted to take advantage of factoryboy while I was at it. However, a slight problem arose – I could not use it due to the way factories are defined: FACTORY_FOR requires the model class to be defined and present in the database upfront. This is not the case since I was not dealing with a concrete models and tables for abstract models are not created.

The first question that I was asked was: Why on earth would you test an abstract model? To be clear – I don’t want to test the model itself – I want to test its methods and I want to do it before they are potentially overriden.

Read more…

Highlighting current active page in Django

Every once in a while, you would like to highlight a current, active page in the navigation. There are number of solutions available to this problem, but none of them was flexible enough for me to use in my projects.

The CurrentPageMiddleware is very useful and saves a lot of time, but its main problem is that it only adds CSS classes to anchor elements, but what if you wanted to add CSS class to its parent element?

Read more…