Play nice with others - Honza Král

Tags: django, djangocon

Many people think that reusable apps don’t work: there’s always something you need to change or modify. Honza is going to talk about his experience with ella, a django CMS.

He advocates using model inheritance. from ella.core.models import Publishable and then subclass your specific model (YoutubeVideo, for instance) from it. That Publishable has most of the basic CMS functionality. That way you get most of what the CMS needs for free and you still can extend it.

Showing the new model? You can use different templates easily. render_to_string() and friends accept a list of templates. So you can give it ['publishable.html', 'youtubevideo.html'] and so, using templates named somewhat after the model. This way you can re-use basic templates, but modify them if you want, just by providing a specially-named template. No code changes necessary.

They’re using Redis to collect information from the Django database on publishables. This way you don’t have any problem with Django’s database’s behaviour of focusing on a single kind of model at a time.

They also use django-appdata for storing extra data on existing Django models. From the pypi page: extandable field and related tools that enable Django apps to extend your reusable app. Through a registry you can add for instance tags to an existing model. To actually see the field in the admin, you do have to make a new ModelAdmin for that . Django-appdata is a hack, but sometimes hacks are very useful.

Through ella.core.custom_urls they even managed to add URLs (like a URL for adding “+1” functionality) to arbitrary models that support it (through django-appdata).

Warning: with great power comes great responsibility. All this is powerful, so it is easy to make a complete and utter mess out of it. Perhaps it is better to convince the customer to forgo a feature?

Warning: keep the defaults sane. Nice to use Redis to make querying quicker and simpler, but you just forced every developer to have Redis installed locally.

Warning: premature optimalization is the root of all evil. Likewise with extensibility. You normally don’t need to make an app extensible. But don’t close the door to extensibility. Add it when you need it.

 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):