[Answered ]-Custom Managers and "through"

2👍

Simplest way is to just add a method to Album (i.e. album.add_photo()) which handles the metadata and manually creates a properly-linked Photo instance.

If you want to get all funky, you can write a custom manager for Photos, make it the default (i.e. first assigned manager), set use_for_related_fields = True on it, and give it an add() method that is able to properly set the default metadata for the relationship.

Aside: seems like it wouldn’t be too hard for Django to make this generic; instead of removing the add() method when there’s a through table, just make add() accept arbitrary kwargs and treat them as data for the through table.

Leave a comment