Get yourself a better Django proxy experience

Ah, Django proxy models and the power they hold! Yet, the implementation aftermath can highlight a number of unwanted side-effects. Here’s a couple of tips when working with Django proxies that will make end-users grateful and developers sigh in relief.

No more toe-curling results from ORM queries

Initialize discriminating proxy model fields to their expected values. You’ll see the benefits of this tip when unit-testing proxy instances already, where you should be using to speed up your tests and straight proxy model queries to shorten your ORM statements.

Start with overriding the main queryset method in :

In :

Sensible link behaviour in the parent changelist

Sometimes you want to keep showing the proxy parent model admin for listing all the results. One issue with this is that every instance link in the changelist still points to its non-proxified version. You will see how problematic this ends up being if not corrected as your proxy modeladmins grow in behavioral diversity. What we can do is add a method to our model returning the right proxy and override in the proxy parent admin model.

In :

In :

As you can see, it doesn’t take that much to make Django proxy models better. Find here the official documentation on the general subject.

Philosophy grad specialised in Journalism and Computer Science, working in product development.