Skip to main content

Posts

How to use Django's Proxy Models

Modeling custom behavior without touching the database While proxy models aren't the most critical feature in the Django framework, they do seem to get short shrift. Here's a look into how to use this feature to create new and clean interfaces to data without making changes to your database. Django’s proxy models are one of those features that I remember reading about and thinking, “oh, cool… I guess” and then moving along. From time to time, they do come in very handy, but there’s not that much written about how to best make use of them What are they? A proxy model is just another class that provides a different interface for the same underlying database model. That’s it. Really. A proxy model is a subclass of a database-table defining model. Typically creating a subclass of a model results in a new database table with a reference back to the original model’s table - multi-table inheritance. A proxy model doesn’t get its own database table. Instead it operates on...