More Metadata With "Through"
I've professed my love of metadata before and one of my new favorite features in Django is the addition of through
on ManyToManyField
relationships. I almost missed this addition when it happened (caught via Michael Trier's post) and it hasn't gotten much attention since, which I think is a bit unfortunate.
A standard ManyToManyField
creates a simple join table with three columns (an id
column and two foreign key columns) which is fine for many cases. However, there are times where you want to track additional information about the relationship. Some example use cases include:
- created/updated times (for generally tracking how "fresh" the relationship is),
- Authors to Blogs/Articles (type of authorship like 'primary', 'guest', 'editor'),
- Users to Songs (which could store ratings, play count, favorite status),
- and of course the obligatory social networking reference in Users (self-referential through Contacts with details about who initiated the contact, acceptance date, how they know each other, et cetera).
I considered providing code samples here of usage but the docs really seem to provide a pretty complete so I don't think there's much I could add of value.
Hopefully, this has gotten the bit of writer's block I've got out of my system. I've been stewing on this for weeks now, and though I don't think I've done it justice, at least I've put it out there because I think it's pretty neat. Onward and upward.