I am writing a small music database. Learning SQL lies quite a long time in my past and I always wanted to give Django a try. But there is one thing I couldn’t wrap my head around.
Right now, my models only consist of two Classes, Album and Song.
Song has a foreign key pointing to the album it belongs to. Now if I would delete that Album, all Songs "belonging" to it, would be deleted due to the cascading effect.
Albums are kinda virtual in my database, only songs are actually represented on the filesystem and the albums are constructed according to the songs tags, therefore I can only know an album doesn’t exist anymore if there are no more songs pointing to it (as they no longer exist in the filesystem).
Or in short, how can I achieve a cascade in reverse, that means, if no more songs are pointing to an album, the album should be deleted as well?
You could use the
pre_deletesignal to remove the Album when a Song is deleted and there are no more songs.